Click here to Skip to main content
15,898,928 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to skip reading log/text files line. Suppose today my log file having 1000 nos of line. I have read it completely already. So i need to keep that line no as bookmarks and i am able to get that line no as well. And next day my log file change and added 2000 no of lines. So i don't want to read it from start i need to read it from 1001 to 2000. So how to do that without using any looping because if i am using looping then its again reading from start to end. Please help me on this.
Posted
Comments
Nathan Minier 16-Mar-15 7:50am    
You could add a little metadata to the file, maybe a header with a "lastLineRead" property so that you know where it was left off the previous day.

That'd require some thought as to certain logging mechanisms, such as clearing, but would work as you're describing.
[no name] 16-Mar-15 13:15pm    
Read this: http://stackoverflow.com/a/21307627/4320056

You can use this:
C#
var lines = File.ReadLines(ofd.FileName);
foreach (string line in lines.Skip(8))
    Trace.WriteLine(line);

XML
Because the File.ReadLines returns an IEnumerable<string>, the lines are only loaded when iterated.

Also check File.ReadLines Method http://msdn.microsoft.com/en-us/library/dd383503.aspx

Happy coding Hours ;_)
 
Share this answer
 
Comments
[no name] 16-Mar-15 8:30am    
"Because the File.ReadLines returns an IEnumerable<string>, the lines are only loaded when iterated."

..Yes - only loaded when iterated. What happens when you .Skip() ?
[no name] 16-Mar-15 13:14pm    
Read this: http://stackoverflow.com/a/21307627/4320056
Mario Z 16-Mar-15 16:53pm    
I don't get how this answer got upvoted and @manchanx downvoted, that doesn't make any sense because this is plain wrong.
Yes it is true that with ReadLines lines are only loaded when iterated, but what do you think that Skip method does, not iterate through lines?

This is literally how Skip extension method looks:

using (IEnumerator<tsource> e = source.GetEnumerator())
{
while (count > 0 && e.MoveNext()) count--;
if (count <= 0)
while (e.MoveNext()) yield return e.Current;
}

In other words you will read each and every single line and each line will create appropriated string value.
The @manchanx second suggestion is a direction to the right path.
Solution 1:
Rename the log file before you read it, e.g. by adding the date into the filename, and then read it. The new entries then get logged into a new empty log file. That way you won't have to track anything.

Solution 2:
When reading the log file you probably use a StreamReader and call ReadLine() repeatedly in a loop? You can get the byte-position of the StreamReader in the file via StreamReader.BaseStream.Position. I'm not 100% sure that this matches the end of the last line read exactly but if it doesn't it will be "very close". Save that position somewhere - in an extra file or in a database - and when you open the logfile the next time, seek the BaseStream ahead to that position before starting to ReadLine().
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900