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.
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.
If the log is only appended, you can try opening a file in read-only mode without a lock. That way other processes can write to it while you read it.
var fs = new FileStream(path,FileMode.Open,FileAccess.Read, FileShare.ReadWrite);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With