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
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.
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....
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.
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/