posted on Wednesday, January 05, 2005 4:29 PM by johnwood

Tail.net - The Unix Tail Command for Windows

So I spent a few minutes searching around for a utility like Unix's Tail utility: something that can view a text file, monitor the file for changes and display updates to the file as they happen.  For example, it's useful when you want to monitor log files for new entries.

Well anyway I only found a couple at best, although I admit I didn't try all that hard. One of them was in the Windows 2003 Resource Kit, which I didn't feel like installing for just such a simple utility. The second was a GUI version and that was pretty much overkill.

It's such a simple utility I decided to write it myself. In fact the executable was just 4k (!) and it took me all of 10 minutes.  The code is also a pretty good example of using the FileSystemWatcher class.

The code is as follows:

public class TailApp

{

            long filePos = 0;

            string filename = null;

            FileSystemWatcher watcher = null;

 

            public void ShowTail()

            {

                        StreamReader reader = new StreamReader(filename);

                        reader.BaseStream.Seek(filePos, SeekOrigin.Begin);

                        filePos = reader.BaseStream.Length;

                        Console.Write(reader.ReadToEnd());

                        reader.Close();

            }

           

            void FileUpdated(object sender, FileSystemEventArgs args)

            {

                        ShowTail();

            }          

           

            public TailApp(string filename)

            {

                        this.filename = filename;

 

                        ShowTail();

                        watcher = new FileSystemWatcher(Path.GetDirectoryName(filename), Path.GetFileName(filename));

                        watcher.NotifyFilter = NotifyFilters.LastWrite;

                        watcher.Changed += new FileSystemEventHandler(FileUpdated);

                        watcher.EnableRaisingEvents = true;

            }

           

            static void Main(string[] args)

            {

                        if (args.Length==0)

                                    Console.WriteLine("Tail.net v0.1.  Usage: tail filename");

                        else

                        {

                                    try

                                    {

                                                TailApp tail = new TailApp(args[0]);

                                                while (true) Console.ReadLine();

                                    }

                                    catch (Exception e)

                                    {

                                                Console.WriteLine("Error: " + e.Message);

                                    }

                        }

            }

}

Or you can download it here: http://www.serviceframework.com/services/tail.zip

Now you can run something like:  "tail c:\mylog.log"- and this will write out the content of the file, and then wait for further updates to the file, showing them as they occur.

Comments