Share via


根據事件記錄到 NT 事件記錄檔

NTEventLogEventConsumer類別會在發生指定的事件時,將訊息寫入 Windows 事件記錄檔。 這個類別是 WMI 提供的標準事件取用者。

注意

根據預設,已驗證的使用者無法將事件記錄到遠端電腦上的應用程式記錄檔。 因此,如果您使用NTEventLogEventConsumer類別的UNCServerName屬性,並將遠端電腦指定為其值,本主題中所述的範例將會失敗。 若要瞭解如何變更事件記錄檔安全性,請參閱此 知識庫文章

 

使用標準取用者的基本程式說明為 使用標準取用者監視和回應事件。 以下是使用 NTEventLogEventConsumer 類別時所需的基本程式以外的其他步驟。 這些步驟說明如何建立寫入應用程式事件記錄檔的事件取用者。

下列程式描述如何建立寫入 NT 事件記錄檔的事件取用者。

建立寫入 Windows 事件記錄檔的事件取用者

  1. 在 Managed 物件格式 (MOF) 檔案中,建立 NTEventLogEventConsumer 的實例,以接收您在查詢中要求的事件。 如需撰寫 MOF 程式碼的詳細資訊,請參閱 設計 Managed 物件格式 (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。 查看應用程式事件記錄檔。 您應該會看到識別碼 = 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";
};

使用標準取用者監視和回應事件