I'm trying to write a console app in C# which reads a log file. The problem that i'm facing is that this log file is updated every 1 hour so for example, if I had 10 lines in the beginning and afterwards 12, in my second read attempt i will have to read only the 2 newly added lines. Can you suggest me a way to do this efficiently (without the need to read all the lines again because the log file usually has 5000+ lines)?
Asked
Active
Viewed 353 times
0
-
If I understand your question correctly, you could try the following: var nextLines = File.ReadLines("filePath").Skip(lineCount); – Sean Sep 09 '15 at 08:45
-
@Sean Thanks for your answer, I already tried this, but a friend told me that it will still read the whole log file, so in a case of 5-10k of lines it might not be that efficient. – Fotis E. Sep 09 '15 at 08:53
-
Perhaps not. I guess it would depend on the size of the file - Garath's answer isn't a bad route to take, either. – Sean Sep 09 '15 at 08:56
1 Answers
2
First of all you can use FileSystemWatcher
to have notifications after file changed.
Morover you can use FileStream
and Seek
function to ready only new added lines. On http://www.codeproject.com/Articles/7568/Tail-NET there is an example with Thread.Sleep
:
using ( StreamReader reader = new StreamReader(new FileStream(fileName,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) )
{
//start at the end of the file
long lastMaxOffset = reader.BaseStream.Length;
while ( true )
{
System.Threading.Thread.Sleep(100);
//if the file size has not changed, idle
if ( reader.BaseStream.Length == lastMaxOffset )
continue;
//seek to the last max offset
reader.BaseStream.Seek(lastMaxOffset, SeekOrigin.Begin);
//read out of the file until the EOF
string line = "";
while ( (line = reader.ReadLine()) != null )
Console.WriteLine(line);
//update the last max offset
lastMaxOffset = reader.BaseStream.Position;
}
}

Piotr Stapp
- 19,392
- 11
- 68
- 116
-
Thank you @Garath, your solution works good for me. I just have a question. I used a Console.WriteLine(lastMaxOffset) to see what value i will get and there's a number e.g 1911. Can you tell me what does this value represent? – Fotis E. Sep 09 '15 at 11:42
-
It is offset in file. So next time file stream starts reading from this value – Piotr Stapp Sep 09 '15 at 12:00