user2443476
user2443476

Reputation: 1975

Detect changes in iis logs files

I am running a filesystemwatcher on the "w3svc1" folder, where the iis logs are stored by default. When I go to the adress localhost or anyone of my webapp localhost/xxxxx. The filesystemwatcher does not raise events. I know that there is a delay between the request and the writting in the logs, but even after one hour there is no change event raised. But then, when I open the file with notepad++, I see the logs added. Is anyone has an explaination. This is my code:

class Program
{
    static void Main(string[] args)
    {
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = @"C:\inetpub\logs\LogFiles\W3SVC1";            
        watcher.NotifyFilter = NotifyFilters.LastWrite;
        watcher.IncludeSubdirectories = true;            
        watcher.Filter = "*.log";            
        watcher.Changed += new FileSystemEventHandler(OnChangedok);
        watcher.Created += new FileSystemEventHandler(OnChangedok);
        watcher.EnableRaisingEvents = true;

        Console.WriteLine("Press \'q\' to quit the sample.");
        while (Console.Read() != 'q') ;


    }

    private static void OnChangedok(object source, FileSystemEventArgs e)
    {            
        Console.WriteLine(e.FullPath);         

    }

Upvotes: 1

Views: 792

Answers (1)

Mahendran
Mahendran

Reputation: 486

This is because IIS open stream and write the logs into file but didn't flush the changes Immediately. When you open the file in notepad++, it access this file which trigger the file modification event . Please refer this thread also.

Upvotes: 2

Related Questions