Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

readfile

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.

like image 682
madu Avatar asked May 01 '13 13:05

madu


2 Answers

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.

like image 111
bland Avatar answered Oct 14 '22 06:10

bland


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);
like image 28
alex Avatar answered Oct 14 '22 06:10

alex