Click here to Skip to main content
15,607,634 members
Articles / General Programming / Tools
Posted 6 Dec 2010


2 bookmarked

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
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?
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.


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 
I just saw this article

, which is a half page article directly copy pasted from internet with 1 page, guess not even single line is you have added, if this can have rating 4 why not mine with 5..

If you are craze about increasing rating of your aticle, do some proper works, i guess code project should ban such articles which is 100% copy paste and nothing new, which just increase their filesystem and waste of time for code project team about posting it...

Try to learn programming, use experience to put in code project, not just copy paste works, this kind of stuffs may help here but not at all for you future my friend.

And one more important thing, try to talk and behave professionally.

I guess your article mentioned there has 4 rating for how to create FileStream, even kids know about, not require a software professional, even it doesn't deserves 0 also, it should be -1...
You have manipulated it to be become 4, are you ethical.. are you following code project standards, are you professional, i guess you are just a college guy came out and joined some small company and posting your articles, just to pose off, you know .NET and C# and use it as a tool to join companies.....
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 
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.