使用WMI监视事件日志

本文关键字:日志 事件 监视 WMI 使用 | 更新日期: 2023-09-27 18:20:04

我正试图使用WMI来监视事件日志中的EntryWritten事件,因此我按如下方式设置了处理程序:

// 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()方法中,但如果我这样做,服务将不会启动。不知道有没有人对此有什么想法?

使用WMI监视事件日志

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

必须使用ISA关键字将要检查的类包含在句子中

类似

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