There is a critical note in MSDN docs that can help you more reliably detect changes:
Keep your event handling code as short as possible.
I suspect (but I donβt know for sure) that this is due to the fact that file system events are raised in the main observer stream, so at any time spent processing events, a window is created in which changes can go unnoticed. As you described your solution, it seems likely that you are doing I / O in the callback (writing to the XML change log file), and this can definitely be too much work to do built-in, according to the criteria of the API Docs. If you have a lot of work to do with an event, release your events for processing in a separate thread so that you can return to viewing the file system as soon as possible.
One relatively easy way to do this is to use ThreadPool.QueueUserWorkItem . This means that your change log will still not be 100% synchronized with the state of the file system (due to the delay caused by using separate threads and queues), but this may be more accurate and seems to be your main problem. You need to make sure that your WaitCallback called by the threadpool file is thread safe (for example, writing to the change log is not performed simultaneously without lock() or the like), and keep in mind that there is no guarantee that change log entries will be written to the order in which they occurred (although it is doubtful that FileSystemWatcher guarantees this anyway).
Also in accordance with the recommendations of the API - make sure that you are filtering a possible set of events, so you only get a callback for those that you absolutely must see:
To avoid buffer overflows, use the NotifyFilter and IncludeSubdirectories properties buttons so you can filter out notifications of unwanted changes.
source share