Click here to Skip to main content
15,867,308 members
Articles / General Programming / Tools
Alternative
Tip/Trick

A C# implementation of the Tail -f command

Rate me:
Please Sign up or sign in to vote.
3.73/5 (3 votes)
19 Dec 2010CPOL 19.7K   2   11
Unless I'm misreading your code on CodePlex, it looks like your utility loads in the entire file every time it changes. That might be fine for tiny files, but it would be a huge performance hit for larger ones (a few megs, for example).Why not do it a bit more simply?using (FileStream str...
Unless I'm misreading your code on CodePlex, it looks like your utility loads in the entire file every time it changes. That might be fine for tiny files, but it would be a huge performance hit for larger ones (a few megs, for example).

Why not do it a bit more simply?
C#
using (FileStream str = new FileStream(f, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete))

I just tested this with a quick console app... You can write to the file from outside without any problems. You can rename it, and the C# code will still function. You can TRY to delete it, but it won't actually disappear until the FileStream is closed (and subsequent attempts to operate on it will fail). For a rolling log situation, that seems to be acceptable.

With this method, you can just keep track of your position in the file, and watch for the stream length to become greater than your current position. You can still use a FileSystemWatcher to catch the rename event, close your stream, and wait for a new file with the original name.

The tool's visible functionality (Parameters, string matching, etc.) could still be very useful... Just need to tweak the internals.

License

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


Written By
Software Developer
United States United States
Ian is a life-long computer programmer who, immediately after earning his B.S. in C.S., was snatched up by a certain Wall Street firm that would explode spectacularly a mere five years later. After fourteen years working for hedge funds, he finally escaped to a certain global tech company that shall remain nameless...

On an unrelated note, he's also a published Sci-Fi/Fantasy author.

Comments and Discussions

 
GeneralMy vote of 1 Pin
kishhr127-Jun-12 21:04
kishhr127-Jun-12 21:04 
QuestionAbout your article Pin
kishhr17-Jun-12 22:01
kishhr17-Jun-12 22:01 
AnswerRe: About your article Pin
thatraja18-Jun-12 8:37
professionalthatraja18-Jun-12 8:37 
GeneralIf you have daily log files, do you need to stop it at midni... Pin
Greyspirit_122-Oct-11 9:31
Greyspirit_122-Oct-11 9:31 
If you have daily log files, do you need to stop it at midnight, detect the new logfile, and start it again?
(BTW, was interesting geting it to work under vb.net 2010)
GeneralRight... What I'm saying, though, is that if you properly sp... Pin
Ian Shlasko6-Dec-10 9:57
Ian Shlasko6-Dec-10 9:57 
GeneralI did not notice so much overead, but feel free to join the ... Pin
Felice Pollano6-Dec-10 9:03
Felice Pollano6-Dec-10 9:03 
GeneralAre you sure about that? Tail.ChangeLoop(), in your code, i... Pin
Ian Shlasko6-Dec-10 8:59
Ian Shlasko6-Dec-10 8:59 
GeneralThanks Ian for testing, Not exactly, the file is onlyloaded ... Pin
Felice Pollano6-Dec-10 7:47
Felice Pollano6-Dec-10 7:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.