#眉標=Enterprise Library #副標=設計模型套件系列(9) #大標=Enterprise Library與Instrumentation #作者=文/圖 王寧疆 ==<反灰>=========== System.Management Microsoft.Practices.ObjectBuilder Microsoft.Practices.EnterpriseLibrary.Logging Microsoft.Practices.EnterpriseLibrary.Common ================ ==<反灰>=========== using System.Management; using System.Management.Instrumentation; ================ ==程式1 =========== [assembly: Instrumented("Root/Default")] [ManagedName("MyInstrumentEvent")] //宣告自訂的WMI事件的名稱 public class MyInstrumentEvent : BaseEvent //令類別繼承自BaseEvent類別 { string data; //存放事件相關資訊的變數 int state; //存放事件狀態的變數 public MyInstrumentEvent(string data) //建構函式 { this.data = data; //將參數存放名稱為data的變數 } public int State //存取事件狀態的屬性 { get { return state; } //傳回事件的狀態 set { state = value; } //設定事件的狀態 } } ================ ==<反灰>=========== using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder; using Microsoft.Practices.ObjectBuilder; using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; ================ ==程式2 =========== public class InstrumentationFactory: ICustomFactory { public object CreateObject(IBuilderContext context, string name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache) //負責建立WMI事件類別的物件的方法 { MyInstrumentation createdObject = new MyInstrumentation(); //建立Provider類別的物件 if (context.Locator != null) //如果context參數的Locator屬性有內容值 { ILifetimeContainer lifetime = context.Locator.Get( typeof(ILifetimeContainer), SearchMode.Local); //取得LifeTimeContainer if (lifetime != null) //如果成功取得LifeTimeContainer { context.Locator.Add(new DependencyResolutionLocatorKey(typeof(MyInstrumentation), name), createdObject); //加入新的Locator物件 lifetime.Add(createdObject); //加入新的LifeTime物件 } } return createdObject; //傳回建立好的物件 } } ================ ==<反灰>=========== using Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation; ================ ==程式3 =========== [InstrumentationListener(typeof(DataInstrumentationListener))] public class DataInstrumentationProvider { [InstrumentationProvider("ConnectionOpened")] //宣告所提供的事件名稱為ConnectionOpened public event EventHandler connectionOpened; //宣告事件 public void FireConnectionOpenedEvent() //負責引發事件的方法 { connectionOpened(this, new EventArgs()); //引發名稱為ConnectionOpened的事件 } } ================ ==<反灰>=========== using Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation; using Microsoft.Practices.EnterpriseLibrary.Logging; using System.Diagnostics; using System.Management; ================ ==程式4 =========== internal class DataInstrumentationListener : InstrumentationListener { public DataInstrumentationListener(bool perfCountersEnabled, bool eventLogEnabled, bool wmiEnabled) : base(perfCountersEnabled, eventLogEnabled, wmiEnabled,new AppDomainNameFormatter()) {} [InstrumentationConsumer("ConnectionOpened")] public void ConnectionOpened(object sender, EventArgs e)//傾聽ConnectionOpened事件是否發生的方法 { if (this.PerformanceCountersEnabled) //如果程式或網頁有啟用記錄效能計數器的設定 { PerformanceCounter pc = new PerformanceCounter("Enterprise Library Data Counters", "Connections Opened/sec", "UseInstrmentation", false); //建立管理Connections Opened/sec效能計數器的PerformanceCounter類別的物件 pc.Increment(); //遞增效能計數器的指標值 } if (this.WmiEnabled) //如果程式或網頁有啟用引發WMI事件的設定 { MyInstrumentEvent evt = new MyInstrumentEvent("ConnectionOpened"); //建立MyInstrumentEvent類別的物件 evt.State = 0; //設定MyInstrumentEvent類別的物件的State屬性 evt.Fire(); //引發MyInstrumentEvent事件 } if (this.EventLoggingEnabled) //如果程式或網頁有記錄Windows事件記錄的設定 { LogEntry logEntry = new LogEntry(); //建立LogEntry類別的物件 logEntry.EventId = 100; //設定LogEntry類別的物件的EventId屬性 logEntry.Priority = 2; //設定LogEntry類別的物件的Priority屬性 logEntry.Message = "ConnectionOpened"; //設定LogEntry類別的物件的Message屬性 logEntry.Categories.Add("General"); //設定LogEntry類別的物件的Category屬性 Logger.Write(logEntry); //透過Logging Application Block提供的 //Logger物件記錄資訊到Windows事件日誌 } } } ================ ==<反灰>=========== using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder; using Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation; using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; using Microsoft.Practices.ObjectBuilder; ================ ==程式5 =========== [CustomFactory(typeof(InstrumentationFactory))] public class MyInstrumentation : IInstrumentationEventProvider, IDisposable { private DataInstrumentationProvider instrumentationProvider; //存放Provider的變數 public MyInstrumentation() //類別建構函式 { instrumentationProvider = new DataInstrumentationProvider(); //建立DataInstrumentationProvider類別的物件 } public MyInstrumentation(bool eventLoggingEnabled, bool wmiEnabled) //類別建構函式 { instrumentationProvider = new DataInstrumentationProvider(); //建立DataInstrumentationProvider類別的物件 } public void RaiseDBConnectEvent() //負責引發ConnectionOpened事件的方法 { this.instrumentationProvider.FireConnectionOpenedEvent(); //由DataInstrumentationProvider類別的//物件引發事件 } public object GetInstrumentationEventProvider() //取得Provider的方法 { return instrumentationProvider; } } ================ ==<反灰>=========== Microsoft.Practices.ObjectBuilder.dll Microsoft.Practices.EnterpriseLibrary.Logging.dll Microsoft.Practices.EnterpriseLibrary.Common.dll IntrumentationLibrary.dll ================ ==<反灰>=========== using IntrumentationLibrary; using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder; ================ ==程式6 =========== protected void btnUseInstrumentation_Click(object sender, EventArgs e) { MyInstrumentation MyObject = EnterpriseLibraryFactory.BuildUp();//建立MyInstrumentation類別的物件 MyObject.RaiseDBConnectEvent(); //引發自訂的WMI事件 } ================