Click here to Skip to main content
15,352,321 members
Articles / Programming Languages / C#
Article
Posted 29 Jun 2004

Stats

105.7K views
1.7K downloads
33 bookmarked

Tail.NET

Rate me:
Please Sign up or sign in to vote.
4.30/5 (19 votes)
29 Jun 2004
How to read changes from log files as they are written to. Similar to "tail" from UNIX.

Introduction

I've often remembered using the "tail" command in Linux and thought I could use the same thing in Windows. I've never been able to find a suitable Win32 replacement for the old utility so I had to roll my own. The only use I've found for the program is monitoring log files.

Using the code

There is very little code required to read the changes from a given file. The most notable piece from below is the way that the FileStream is created to read files that may be open by other processes. If you try to use the StreamReader's constructor to open a file in use by another process, you will get an exception. Of course, being able to read open files was crucial to this utility.

C#
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;
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Taylor Wood
Web Developer
United States United States
I write c#

Comments and Discussions

 
QuestionQuestion about the added lines Pin
Member 108553065-Jun-14 8:10
MemberMember 108553065-Jun-14 8:10 
QuestionLicense? Pin
braneof28-May-14 9:00
Memberbraneof28-May-14 9:00 
QuestionWhy seek each time? Pin
Wernight28-Jun-13 0:05
MemberWernight28-Jun-13 0:05 
AnswerRe: Why seek each time? Pin
Baruch Burstein29-Oct-13 3:51
MemberBaruch Burstein29-Oct-13 3:51 
Generalsimple and usefull Pin
madhu_tech10-Sep-12 23:32
Membermadhu_tech10-Sep-12 23:32 
GeneralThanks Pin
Sk8tz26-Sep-10 9:25
professionalSk8tz26-Sep-10 9:25 
GeneralThanks! Pin
Chris Noffke8-Oct-07 10:00
MemberChris Noffke8-Oct-07 10:00 
Questionhello Pin
fairchild11810-May-06 19:04
Memberfairchild11810-May-06 19:04 
GeneralBetter method - FileSystemEventHandler Pin
Member 16197585-Jun-05 12:27
MemberMember 16197585-Jun-05 12:27 
GeneralRe: Better method - FileSystemEventHandler Pin
RichNFamous9-Feb-07 5:38
MemberRichNFamous9-Feb-07 5:38 
GeneralRe: Better method - FileSystemEventHandler Pin
Wernight27-Jun-13 23:56
MemberWernight27-Jun-13 23:56 
GeneralRe: Better method - FileSystemEventHandler Pin
Baruch Burstein29-Oct-13 3:52
MemberBaruch Burstein29-Oct-13 3:52 
GeneralVery Important Code Pin
ntorrisi11-Feb-05 22:04
Memberntorrisi11-Feb-05 22:04 
QuestionHow about trying UnxUtils... Pin
Michael D Bray5-Jul-04 15:18
MemberMichael D Bray5-Jul-04 15:18 
AnswerRe: How about trying UnxUtils... Pin
Eric Engler20-Jul-04 12:29
MemberEric Engler20-Jul-04 12:29 

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.