基于事件记录到 NT 事件日志

NTEventLogEventConsumer 类在发生指定的事件时将消息写入 Windows 事件日志。 此类是 WMI 提供的标准事件使用者。

注意

默认情况下,经过身份验证的用户无法将事件记录到远程计算机上的应用程序日志中。 因此,如果你使用 NTEventLogEventConsumer 类的 UNCServerName 属性并指定远程计算机作为其值,则本主题中所述的示例将失败。 若要了解如何更改事件日志安全性,请参阅此知识库文章

 

使用标准使用者监视和响应事件中介绍了使用标准使用者的基本过程。 下面是除了基本过程之外,在使用 NTEventLogEventConsumer 类时需要执行的其他步骤。 这些步骤说明如何创建写入应用程序事件日志的事件使用者。

以下过程说明如何创建写入 NT 事件日志的事件使用者。

创建写入 Windows 事件日志的事件使用者

  1. 在托管对象格式 (MOF) 文件中,创建 NTEventLogEventConsumer 的实例以接收在查询中请求的事件。 有关编写 MOF 代码的详细信息,请参阅设计托管对象格式 (MOF) 类

  2. 创建并命名 __EventFilter 的实例,然后创建查询以指定触发写入 NT 事件日志的事件类型。

    有关详细信息,请参阅使用 WQL 进行查询

  3. 创建 __FilterToConsumerBinding 的实例以将筛选器与 NTEventLogEventConsumer 的实例相关联。

  4. 使用 Mofcomp.exe 编译 MOF 文件。

示例

此部分的示例采用 MOF 代码,但你可以使用 WMI 的脚本 APIWMI 的 COM API 以编程方式创建实例。 该示例演示如何使用 NTEventLogEventConsumer 创建使用者以写入应用程序事件日志。 MOF 创建名为“NTLogCons_Example”的新类,这是一个事件筛选器,用于在此新类的实例上查询操作(例如创建),以及查询筛选器和使用者之间的绑定。 由于 MOF 中的最后一个操作是创建 NTLogCons_Example 实例,因此你可以通过运行 Eventvwr.exe 立即在应用程序事件日志中查看该事件。

EventID=0x0A for SourceName="WinMgmt" 使用以下文本标识消息。 “%1”、“%2”、“%3”是 InsertionStringTemplates 数组中指定的相应字符串的占位符。

Event filter with query "%2" could not be [re]activated in 
namespace "%1" because of error %3. Events may not be delivered 
through this filter until the problem is corrected.

以下过程说明如何使用该示例。

使用示例

  1. 将以下 MOF 列表复制到文本文件中,并以 .mof 扩展名保存它。

  2. 在命令窗口中,使用以下命令编译 MOF 文件:

    Mofcompfilename**.mof**

  3. 运行 Eventvwr.exe。 查看应用程序事件日志。 你应会看到 ID = 10 (EventID)、Source = "WMI"、Type = Error 的事件。

  4. 在“事件”列中双击来自 WMI 的包含 10 的“信息类型”消息。 将显示事件的以下说明。

    Event filter with query "STRING2" could not be [re]activated in 
    namespace "STRING1" because of error STRING3. Events cannot be 
    delivered through this filter until the problem is corrected.
    
// Set the namespace as root\subscription.
// The NTEventLogEventConsumer is already
// compiled in the root\subscription namespace. 

#pragma namespace ("\\\\.\\Root\\subscription")
class NTLogCons_Example
{
 [key] string name;
 string InsertionString;
};

// Create an instance of the NT Event log consumer
// and give it the alias $CONSUMER

instance of NTEventLogEventConsumer as $CONSUMER
{
    // Unique instance name
    Name = "NTConsumerTest"; 
    // System component that generates the event
    SourceName = "WinMgmt";
    // Event message WBEM_MC_CANNOT_ACTIVATE_FILTER
    EventID = 0xC000000A;
    // EVENTLOG_ERROR_TYPE
    EventType = 1;
    // WMI event messages do not have multiple categories
    Category = 0;
    // Number of strings in InsertionStringTemplates property
    NumberOfInsertionStrings = 3;

    InsertionStringTemplates =
       {"%TargetInstance.Name%",
        "%TargetInstance.InsertionString%",
        "STRING3"};
};

// Create an instance of the event filter
// and give it the alias $FILTER
// The filter queries for any instance operation event
// for instances of the NTLogCons_Example class

instance of __EventFilter as $FILTER
{
    // Unique instance name
    Name = "NTLogConsFilter";
    Query = "SELECT * from __InstanceOperationEvent"
            " WHERE TargetInstance ISA \"NTLogCons_Example\"";
    QueryLanguage = "WQL";
};

// Create an instance of the binding
// between filter and consumer instances.

instance of __FilterToConsumerBinding
{
    Consumer = $CONSUMER;
    Filter = $FILTER;
};

// Create an instance of this class right now. 

instance of NTLogCons_Example
{
   Name = "STRING1";
   InsertionString = "STRING2";
};

使用标准使用者监视和响应事件