C#: tail like program for text file

2019-02-09 08:02发布

I have a log file that continually logs short lines. I need to develop a service that reacts (or polls, or listens to) to new lines added to that file, a sort of unix' tail program, so that my service is always up to date reguarding the file.

I don't think that opening a read stream and keeping it opened is a good idea. Maybe I should use the FileSystemWatcher class.

Long story short, I need to parse in real time every new line added to this file.

Any idea help or indication is really appreciated.

EDIT

As I've been not very clear. I do not need any program, I am writing a program. For reading (then processing) every new line added to the file. I mean that what I'm looking for is a methodology (or: how to implement this?) for continually tailing a file that keeps on been written.

I have to develop a Windows service that "listens" to this file and does operations on every new line.

So, if in a given moment the file is:

12.31.07 - jdoe [log on] 347
12.32.08 - ssmith [log on] 479
12.32.08 - mpeterson [log off] 532
12.32.09 - apacino [log on] 123

in the very moment that the line

12.32.11 - pchorr [log on] 127

is added to the log file by the logging program (that I have not access to), I need my Windows service to "react" to the line addiction, intercept the new line (12.32.11 - pchorr [log on] 127) and process it. And so on.

Now, I don't know how to do this. I should poll the file every n seconds, store the last read line in memory and process only the newly added lines. The problem with this is that is very slow, plus I'd be reading a very large file every time.

Or maybe I could use FileSystemWatcher, but I haven't found any example of using it for similar purposes.

So, what would you suggest to get the work done? Thanks.

4条回答
贼婆χ
2楼-- · 2019-02-09 08:07

Simple solution would be use , sample code provided in http://www.codeproject.com/Articles/7568/Tail-NET article. It is just one function copy/paste into your code.

查看更多
Lonely孤独者°
3楼-- · 2019-02-09 08:12

You haven't really explained whether you need a tail-like program for Windows i.e. http://www.baremetalsoft.com/baretail/ or if you want a windows version of tail (use cygwin) or if you're looking for some sort of log monitoring API....

查看更多
不美不萌又怎样
4楼-- · 2019-02-09 08:15

I would recommend using FileSystemWatcher to be notified of changes to the file or files you're concerned about. From there, I would cache information such as the size of the file between events and add some logic to only respond to full lines, etc. You can use the Seek() method of the FileStream class to jump to a particular point in the file and read only from there. Given these features, it shouldn't be too hard to hand-roll this functionality if that's what you need.

查看更多
神经病院院长
5楼-- · 2019-02-09 08:21

It is important to note that Microsoft (since vista/svr08) no longer updates file metadata when a file is updated (such as a log file being updated by a service).

For example, the metadata for a file such as modified date, will not be updated until the file is closed by the service/program which is updating the log file.

Therefore FileSystemWatcher will NOT catch log file updates as you might expect.

https://blogs.technet.microsoft.com/asiasupp/2010/12/14/file-date-modified-property-are-not-updating-while-modifying-a-file-without-closing-it/

查看更多
登录 后发表回答