I want to track the log file of our PBX for changes. I made a small program that does this only with FileSystemWatcher .
Now it gets weird: FileSystemWatcher never starts Changed -Event when I just run the program. Although the log file has really changed. But when I open the directory in Windows Explorer, where the log file is located, the program works as expected. But only as long as the browser window remains open ... what is it ...?
Operating System: Windows Server 2008 R2
EDIT: Sorry, here is the code:
class Program { static void Main(string[] args) { new LogFileWatcher(@"C:\PBX\Dial.log"); System.Console.Read(); } } public class LogFileWatcher { public string LogFilePath { get; private set; } private DateTime _lastLogFileWriteTime; public LogFileWatcher(string path) { LogFilePath = path; var directoryName = Path.GetDirectoryName(LogFilePath); var fileName = Path.GetFileName(LogFilePath); var fsw = new FileSystemWatcher { Path = directoryName, Filter = fileName }; fsw.Changed += fsw_Changed; fsw.EnableRaisingEvents = true; } private void fsw_Changed(object sender, FileSystemEventArgs e) {
EDIT2: Perhaps this is important: the log file is used by the Windows PBX service! I can open it using Notepad.
source share