2011-07-08 42 views
4

我想監視我們的PBX的日誌文件的變化。我做了一個小程序,只用FileSystemWatcherFileSystemWatcher有一個奇怪的行爲

現在它變得很奇怪:FileSystemWatcher從不會觸發Changed-當我簡單地啓動程序時發生。儘管日誌文件確實發生了變化。但是,當我打開日誌文件所在的Windows資源管理器中的目錄時,該程序按預期工作。但只要瀏覽器窗口保持打開狀態......什麼是..?

操作系統:在Windows Server 2008 R2

編輯:對不起,這裏是代碼:

class Program 
{ 
    static void Main(string[] args) 
    { 
     new LogFileWatcher(@"C:\PBX\Dial.log"); 
     System.Console.Read(); 
    } 
} 

public class LogFileWatcher 
{ 
    public string LogFilePath { get; private set; } 

    private DateTime _lastLogFileWriteTime; 

    public LogFileWatcher(string path) 
    { 
     LogFilePath = path; 
     var directoryName = Path.GetDirectoryName(LogFilePath); 
     var fileName = Path.GetFileName(LogFilePath); 

     var fsw = new FileSystemWatcher { Path = directoryName, Filter = fileName }; 
     fsw.Changed += fsw_Changed; 
     fsw.EnableRaisingEvents = true; 
    } 

    private void fsw_Changed(object sender, FileSystemEventArgs e) 
    { 
     // Get and fix the last write time of the log file 
     var fixLastWriteTime = File.GetLastWriteTime(LogFilePath); 

     // Don't do anything when file didn't change since last time 
     if (fixLastWriteTime == _lastLogFileWriteTime) return; 

     Console.WriteLine("File changed on: {0} - ID:{1}", DateTime.Now.ToLongTimeString(), Guid.NewGuid()); 

     // Save last write time of the log file 
     _lastLogFileWriteTime = fixLastWriteTime; 
    } 
} 

EDIT2:也許這是很重要的:日誌文件是在由PBX Windows的服務使用!我可以用記事本打開它。

+0

你有什麼設置FileSystemWatcher的'Path'屬性? –

+0

如果您向我們展示了您使用的代碼,它將幫助我們理解您的問題。 –

+1

嘗試設置'FileSystemWatcher.NotifyFilter'屬性:http://msdn.microsoft.com/en-us/library/system.io.notifyfilters.aspx – Bolu

回答