2012-01-12 89 views
0

我试图使用WMI来监视事件日志EntryWritten事件,所以我成立了处理程序如下:使用WMI监视事件日志

// Create the event log monitor 
     string query = "Select * From __InstanceCreationEvent Where TargetInstance.LogFile='Application'"; 
     WqlEventQuery aProcessCreationQuery = new WqlEventQuery(query); 
     ManagementEventWatcher aWatcher = new ManagementEventWatcher(aProcessCreationQuery); 
     aWatcher.EventArrived += new EventArrivedEventHandler(EventLogMonitor); 

但我的处理方法EventLogMonitor永远不会触发,即使事情正在写入应用程序事件日志。我的应用程序作为一项服务监控,用于写入事件日志的事物。

我发现的地方,我可能需要添加一行:

aWatcher.Start() 

在StartService()方法,但如果我做的服务无法启动。我想知道有人对此有任何想法吗?

回答

3

您WQL senence是错误的 Select * From __InstanceCreationEvent Where TargetInstance.LogFile='Application'

您必须包括您在句中使用ISA关键字

的东西,像这样

Select * From __InstanceCreationEvent WHERE TargetInstance ISA 'Win32_NTLogEvent' and TargetInstance.LogFile='Application'

检查类