1

我在“w3svc1”文件夹上运行一个文件系统观察程序,默认情况下存储 iis 日志。当我转到地址 localhost 或我的 webapp localhost/xxxxx 的任何人时。文件系统观察器不会引发事件。我知道请求和写入日志之间存在延迟,但即使在一小时后也没有引发任何更改事件。但是,当我用 notepad++ 打开文件时,我看到添加了日志。有没有人解释一下。这是我的代码:

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);         

    }
4

1 回答 1

2

这是因为 IIS 打开流并将日志写入文件但没有立即刷新更改。当您在 notepad++ 中打开文件时,它会访问该文件,从而触发文件修改事件。也请参考这个线程

于 2015-02-23T07:35:47.983 回答