1

I am watching for changes in a .log file that is being written by a software of an industrial machine.

The software is continously writing but no changes are detected by FileSystemWatcher.

However, when I open the Windows Explorer and navigate to the watched directory, suddenly FileSystemWatcher detects inmmediatly the last change and fires. (quite strange).

I am configuring the watcher as:

FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = "C:\\nextgen\\log";
            
watcher.NotifyFilter = NotifyFilters.DirectoryName |
           NotifyFilters.LastAccess |
           NotifyFilters.LastWrite |
           NotifyFilters.FileName |
           NotifyFilters.Size |
           NotifyFilters.Attributes | NotifyFilters.CreationTime;
// Only watch logfiles.
watcher.Filter = "*.log";

// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created+= new FileSystemEventHandler(OnChanged);
// Begin watching.
watcher.EnableRaisingEvents = true;
E. Williams
  • 405
  • 1
  • 6
  • 21
  • 1
    https://stackoverflow.com/questions/9563037/filesystemwatcher-changed-event-for-lastwrite-is-unreliable – Mitch Wheat Nov 19 '21 at 08:56
  • it seems that file changes are cached in nfts filesystem. so won't be capable of detecting realtime file changes. is there a way to workaround this? Maybe developing my own filewatcher? – E. Williams Nov 19 '21 at 09:10
  • indeed it says it. thank you for the post. Now I am asking for a workaround. It is quite weird that Windows doesn't offer a low-level api to monitor this caching or to workaround it – E. Williams Nov 19 '21 at 09:15
  • I don't know of a work around, sorry. Given the file is being written continuously, you know you have constant changes, so maybe you have an XY problem. – Mitch Wheat Nov 19 '21 at 09:24
  • 1
    Since is impossible to capture file changes: Solved using a thread and actively reading the file every 20 seconds. – E. Williams Nov 19 '21 at 16:36

0 Answers0