【发布时间】:2013-02-12 18:05:34
【问题描述】:
我在 Windows 服务中有一个 FileSystemWatcher 对象。我将它写在一个控制台应用程序中,使用该应用程序作为我的组件的存根。
我的组件实例化 FileSystemWatcher 并设置为监视映射的驱动器。从测试存根控制台应用程序和可部署的 Windows 服务来看,这对我来说非常有用。
我也有与 log4net 连接的 onError 事件,记录了一个致命级别的错误:
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 服务中运行时,此日志错误将不起作用。
有人知道为什么或如何解决吗?
【问题讨论】:
-
您是否从
OnStart创建 FileSystemWatcher 实例?创建参考后如何处理它?它可能会过早收集,因此该事件永远不会触发。不过只是猜测。
标签: .net windows-services filesystemwatcher