C# - Most effective way to periodically read last part of a file

c#, readfile

Solution

I think you're looking for this, where offset is going to be how much you want backtrack. Reference: MSDN

using (FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read))
{
    fs.Seek(offset, SeekOrigin.End);
}

Now the filestream is pointing to however far back in the file you set 'offset' to be, and you can read from there.

Problem

I want to periodically read a log file that is also being written to. The program will be periodically reading the log file contents and parsing it to extract some values. But I do not want to read the whole file each time. Is there a way to read a file from a particular line onwards? For example at the first read the file has 100 lines. I note this value and next time I read I start reading from line 100 onwards and store the line number of the current file. Is there an efficient way to do this? The log file will grow to about 100MB and I need to read about every 5 seconds. So it wouldn't be that efficient to read the complete file each time. Any suggestion is greatly appreciated.

Original source