Question

A quick explanation. I have a game server that write a console.log file in run time. I am trying to make an event that reads the file when new changes are made.

If i save the document from a text editor my changeevent runs fine. But not when the server writes to it.

When i open the log file in run time, i can see that all the lines is in the document is getting written fine.

Why don't i get the event invoked?

Code:

class Collect
{
    public FileSystemWatcher watcher = new FileSystemWatcher("C:\\.q2online\\action\\logs\\");

    public Collect()
    {
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        watcher.Changed += new FileSystemEventHandler(OnChanged);

        watcher.Filter = "console.log"; 
        watcher.EnableRaisingEvents = true;
    }

    private static void OnChanged(object scource, FileSystemEventArgs e)
    {
        Debug.WriteLine(ReadLastLineFromFile(DataContainer.path.Default.logFilePath));
    }
Was it helpful?

Solution

Couple of things. 1. It is possible to turn off last access time updates on NTFS file systems. Many servers do this to reduce file system activity. Windows also may delay for up to 1 hour writing last access time. So getting real-time updates based on last access will generally be unreliable. However, you are looking at other attributes so this is likely not an issue but would be if that were the only attribute you were filtering on.

Note also that the last modified time is not updated UNTIL the file system handle is closed. If your server code is holding the handle open and only appending to the file perhaps this could be an issue. If you control the server side code then check to make sure you close the file handle after each write. Here is an article with some useful info: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724290(v=vs.85).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top