4

I am stating to write a little PC tool to read log files using c# or java. The log files will be in .txt format. An application is running and writing logs, and I want my tool to open the log at the same time and refresh automatically when a new line is written to the log file.

My challenge is, how do I detect the log file changes so that my tool will have real-time displaying ability? This is a general question but pseudo codes will be greatly appreciated!

jamesdeath123
  • 4,268
  • 11
  • 52
  • 93
  • 1
    grab some gnu source and look at the `tail` app, which does exactly what you want with the `-f` option. – Marc B Nov 01 '12 at 20:56
  • The [`FileSystemWatcher`](http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx) class might help. Or you can set a timer and poll the length. – Ben Voigt Nov 01 '12 at 20:57
  • 1
    have you thought about using the FileWatcher Class her is a good link for starters. http://www.techrepublic.com/article/use-the-net-filesystemwatcher-object-to-monitor-directory-changes-in-c/6165137 – MethodMan Nov 01 '12 at 20:57

3 Answers3

8

You can use the FileSystemWatcher class (MSDN page). Be careful though, if you try to open the file while the other process is writing the file you will probably be denied access.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
SomeWritesReserved
  • 1,075
  • 7
  • 17
4

There's a bunch of this questions here, and answers too:

Reading file content changes in .NET

Your target is FileStream object, I suppose.

Community
  • 1
  • 1
deb0rian
  • 966
  • 1
  • 13
  • 37
1

I would poll the file and check it's metadata for the last changed date, or if you're using .NET, you could use the FileSystemWatcher class

khellang
  • 17,550
  • 6
  • 64
  • 84