イベントに基づくログ ファイルへの書き込み

LogFileEventConsumer クラスは、特定のイベントが発生したときに、定義済みのテキストをログ ファイルに書き込むことができます。 このクラスは、WMI が提供する標準的なイベント コンシューマーです。

標準コンシューマーを使用するための基本的な手順は常に同じであり、「標準コンシューマーを使用したイベントの監視と対応」に記載されています。

次の手順は基本手順の追加であり、LogFileEventConsumer クラスに固有です。ここでは、プログラムを実行するイベント コンシューマーを作成する方法について説明します。

ログ ファイルに書き込むイベント コンシューマーを作成する方法

  1. Managed Object Format (MOF) ファイルで、LogFileEventConsumer のインスタンスを作成してクエリで要求したイベントを受信し、Name プロパティでインスタンスに名前を付け、ログ ファイルへのパスを Filename プロパティに配置します。

    詳細については、「管理オブジェクトフォーマット (MOF) クラスの設計」を参照してください。

  2. ログ ファイルに書き込むテキスト テンプレートを Text プロパティに指定します。

    詳細については、「標準文字列テンプレートの使用」を参照してください。

  3. __EventFilter のインスタンスを作成し、コンシューマーをアクティブ化するイベントを指定するクエリを定義します。

    詳細については、「WQL を使用したクエリの実行」を参照してください。

  4. フィルターを LogFileEventConsumer のインスタンスに関連付ける __FilterToConsumerBinding のインスタンスを作成します。

  5. ログ ファイルの読み取りまたは書き込みを行うユーザーを制御するには、ログが配置されているディレクトリのセキュリティを必要なレベルに設定します。

  6. Mofcomp.exe を使用して MOF ファイルをコンパイルします。

このセクションのサンプルは MOF コードですが、WMI 用のスクリプト API または WMI 用の COM API を使用してプログラムでインスタンスを作成することができます。 この例では、標準の LogFileEventConsumer を使用して LogFileEvent という名前のコンシューマー クラスを作成します。このクラスは、LogFileEvent クラスのインスタンスが作成されたときにファイル c:\Logfile.log に行を書き込みます。

サンプルの使用方法を次の手順で説明します。

サンプルを使用する方法

  1. 以下の MOF リストをテキスト ファイルにコピーし、拡張子 .mof で保存します。

  2. コマンド ウィンドウで、次のコマンドを使用して MOF ファイルをコンパイルします。

    Mofcomp filename**.mof**

  3. Logfile.log を開き、LogFileEvent.Name が "Logfile Event Consumer event" で指定された行を確認します。

// Set the namespace as root\subscription.
// The LogFileEventConsumer is already compiled 
//     in the root\subscription namespace. 

#pragma namespace ("\\\\.\\Root\\subscription")

class LogFileEvent
{
    [key]string Name;
};

// Create an instance of the standard log
// file consumer and give it the alias $CONSUMER

instance of LogFileEventConsumer as $CONSUMER
{
    // If the file does not already exist, it is created.
    Filename = "c:\\Logfile.log";  
    IsUnicode = FALSE;
    // Name of this instance of LogFileEventConsumer
    Name = "LogfileEventConsumer_Example";  
    // See "Using Standard String Templates"; 
    Text = "%TargetInstance.Name%"; 
    // TargetInstance is the instance of LogFileEvent 
    //    requested in the filter        
                                         
};

// Create an instance of the event filter 
// and give it the alias $FILTER
// Query for any instance operation type,
// such as instance creation, for LogFileEvent class

instance of __EventFilter as $FILTER
{
    Name = "LogFileFilter";
    Query = "SELECT * FROM __InstanceOperationEvent "
        "WHERE TargetInstance.__class = \"LogFileEvent\"";
    QueryLanguage = "WQL";
};

// Create an instance of the binding.

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

// Create an instance of this class right now
// Look at the file c:\Logfile.log to see
// that a line has been written.

instance of LogFileEvent
{
 Name = "Logfile Event Consumer event";  
};

標準コンシューマーを使用したイベントの監視と応答