管理イベントの公開
アプリケーションを管理できるようにするために、管理イベントを発生させる条件をアプリケーション内で指定できます。この条件によって、エラー条件やステータスの変更など、なんらかの事象が起こったことをコンシューマに対して通知します。
System.Management.Instrumentation 名前空間には、管理イベントを公開するためのクラスがいくつか用意されています。イベントを公開するには、次のようにします。
- クラスを InstrumentationClass(InstrumentationType.Event) 属性でマークし、管理イベントとして指定します。
- 属性を使用する代わりに、BaseEvent クラスからクラスを派生させ、そのクラスを管理イベントとして指定します。この方法は、パフォーマンスの観点でも効率的です。
イベントを発生させるには、イベント クラスのインスタンスを作成し、プロパティの値を設定し、ヘルパ メソッド Instrumentation.Fire() または BaseEvent::Fire() メソッドを使用します。どちらのメソッドを使用するかは、イベントの宣言にどちらのメソッドを選択したかによって決まります。
BaseEvent クラスから派生して管理イベント クラスを作成し、マネージ コードから管理イベントを発生させる方法を次のコード例に示します。
using System;
using System.Management;
using System.Configuration.Install;
using System.Management.Instrumentation;
// This example demonstrates how to create a management event class by deriving
// from the BaseEvent class and to raise a management event from managed code.
// Specify which namespace the management event class is created in
[assembly:Instrumented("Root/Default")]
// Let the system know you will run InstallUtil.exe utility against
// this assembly
[System.ComponentModel.RunInstaller(true)]
public class MyInstaller : DefaultManagementProjectInstaller {}
// Create a management instrumentation event class
public class MyEvent : System.Management.Instrumentation.BaseEvent {
public string Event_Name;
}
public class Sample_EventProvider {
public static int Main(string[] args) {
MyEvent e = new MyEvent();
e.Event_Name = "Hello";
// Raise a management event
Instrumentation.Fire(e);
return 0;
}
}
InstrumentationClass 属性を使用して管理イベント クラスを作成し、管理コードから管理イベントを発生させる方法を次のコード例に示します。
using System;
using System.Management;
using System.Configuration.Install;
using System.Management.Instrumentation;
// This example demonstrates how to create a management event class by using
// the InstrumentationClass attribute and to raise a management event from
// managed code.
// Specify which namespace the management event class is created in
[assembly:Instrumented("Root/Default")]
// Let the system know you will run InstallUtil.exe utility against
// this assembly
[System.ComponentModel.RunInstaller(true)]
public class MyInstaller : DefaultManagementProjectInstaller {}
// Create a management instrumentation event class
[InstrumentationClass(InstrumentationType.Event)]
public class MyEvent {
public string Event_Name;
}
public class WMI_InstrumentedEvent_Example {
public static void Main() {
MyEvent e = new MyEvent();
e.Event_Name = "Hello";
// Raise a management event
Instrumentation.Fire(e);
return;
}
}
参照
System.Management を使用する .NET Framework アプリケーションの実装 | CLI と WMI におけるクラスとマップ | 管理データの公開 | 継承 | 実装されたアプリケーションのスキーマ登録