我在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