我在Windows服務中有一個FileSystemWatcher對象。我在使用該應用程序的控制檯應用程序中將它編寫爲我的組件的存根。FileSystemWatcher OnError不適用於Windows服務
我的組件實例化FileSystemWatcher並設置爲觀察映射的驅動器。這對測試存根控制檯應用程序和可部署Windows服務都非常適用。
我也onerror事件迷上了log4net的日誌記錄一個FATAL級別的錯誤:
public FileSystemWatcher CreateFileWatcher()
{
FileSystemWatcher watcher = new FileSystemWatcher();
try
{
log.Info("Configuring DPIFileWatcher");
watcher.Filter = "*.xml";
log.Info("DPIFileWatcher Filter set to: *.xml");
string watcherPath = ConfigurationManager.AppSettings["InoundPath"].ToString();
watcher.Path = watcherPath;
log.Info(String.Format("DPIFileWatcher Path set to: {0}", watcherPath));
watcher.Created += new FileSystemEventHandler(DPIWatcher_FileCreated);
watcher.Changed += new FileSystemEventHandler(DPIWatcher_FileChanged);
watcher.Error += new ErrorEventHandler(DPIWatcher_Error);
watcher.EnableRaisingEvents = true;
log.Info("DPIFileWatcher Configuration Successful.");
}
catch(Exception e)
{
log.Fatal(String.Format("Failed to configure DPIFileWatcher: {0}", e.Message));
}
return watcher;
}
這是我的錯誤事件:
private void DPIWatcher_Error(object source, ErrorEventArgs e)
{
log.Fatal(String.Format("FileWatacher error: {0}", e.GetException().Message));
}
如果我拔掉測試網絡誤差損失網卡,我從我的控制檯應用程序得到以下日誌錯誤:
FATAL [ 12] 2013-02-12 12:14:02,142 SMILLER-E6430 METHOD: DPIWatcher_Error GenFileWatch.DPIFileWatcher- FileWatacher error: The specified network name is no longer available (C:\Development\GenFileWatch\GenFileWatch\DPIFileWatcher.cs: 447)
但硫從Windows服務中運行時,s日誌錯誤將不起作用。
有沒有人有任何想法爲什麼或如何解決?
它記錄什麼? – BNL 2013-02-12 18:07:59
您是否從'OnStart'創建FileSystemWatcher實例?在創建它之後你對這個參考做了什麼?它可能會過早收集,因此事件永遠不會發生。只是一個猜測。 – 2013-02-12 18:57:54