Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect real-time change of text files?

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!

like image 985
jamesdeath123 Avatar asked Dec 12 '22 21:12

jamesdeath123


2 Answers

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.

like image 52
SomeWritesReserved Avatar answered Dec 27 '22 10:12

SomeWritesReserved


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

Reading file content changes in .NET

Your target is FileStream object, I suppose.

like image 25
deb0rian Avatar answered Dec 27 '22 10:12

deb0rian