3.4 [ Enterprise Library ]度量机制(Instrumentation )

版权所有(c) 2007 Dorian Deng。署名 共享 非商用。
转载请注明出处: http://www.cnblogs.com/doriandeng/archive/2007/10/04/913702.html

在 Enterprise Library 中,触发一个事件和在事件触发时将命令( dictate )发生的动作的代码之间是分离的。触发事件的应用程序代码就像提供程序一样,响应事件的代码像 监听程序一样。这种分离允许你在特定事件触发时不需要重编译提供程序代码就能改变将发生的事情,然而,你依然不得不重新编译监听程序代码。

 

当感兴趣的事发生时,应用程序代码触发事件,例如,连接到数据库或记录日志消息。触发这些事件调用在运行时由反射关联的监听器。监听器包含决定在事件触发时将发生什么的代码,如记录消息到事件日志,或者增长计数器。如果没有监听器与应用程序块关联,它的功能普通的。

使用属性

需要下面的三个属性来度量应用程序和监听器:

  • [InstrumentationListener]

  • [InstrumentationProvider]

  • [InstrumentationConsumer]

[InstrumentationListener]属性在提供程序类前出现,这告诉它哪种监听程序类将被实例化。[InstrumentationProvider]属性出现在提供程序代码中的事件前,提供程序代码是在应用程序中的。[InstrumentationConsumer] 出现在监听程序中的处理方法前,在此属性中的指定的名称必须与在 [InstrumentationProvider] 属性中指定的名称匹配。这些属性都被记录为无数据。

下列示例展示了度量一个监听程序的代码。

例 3.1. C#

public class MyListener
{
public MyListener(string instanceName, bool a, bool b, bool c) {}

[InstrumentationConsumer("DbConnect")]
public void ConnectObserved(object sender, EventArgs e)
{
Console.WriteLine("I saw a Database Connect.");
}
}

例 3.2. Visual Basic .NET

Public Class MyListener 
Public Sub New(ByVal instanceName As String, ByVal a As Boolean, ByVal b As Boolean, ByVal c As Boolean)
End Sub

<InstrumentationConsumer("DbConnect")>
Sub ConnectObserved(ByVal sender As Object, ByVal e As EventArgs)
Console.WriteLine("I saw a Database Connect.")
End Sub
End Class

下列代码展示了度量提供程序的代码。

例 3.3. C#

[InstrumentationListener(typeof (MyListener))]
public class MyApplication
{
[InstrumentationProvider("DbConnect")]
public event EventHandler<EventArgs> OnDbConnect;
}

例 3.4. Visual Basic .NET

<InstrumentationListener(GetType(MyListener))> 
Public Class MyApplication
<InstrumentationProvider("DbConnect")>
Public Event OnDbConnect As EventHandler
End Class

当系统实例化 MyApplication 类时,它检测属性确定这是否是一个类型为 MyListener 的监听程序,如果是,它也将实例化那个类。然后它检测在 MyApplication 实例中的所有用[InstrumentationProvider]属性标记的事件;它还在 MyListener 实例中用 [InstrumentationConsumer] 属性标记的方法。它关联在各自的属性中有匹配的字符串"DbConnect" 的事件和方法。这在提供程序和监听程序之间是一一对应的,一个提供程序与一个监听程序关联。

度量组件

应用程序由多个组件组成,如数据库。如果是这种情况,组件代替应用程序本身被度量。要委托度量任务到组件,应用程序必须实现 IInstrumentationEventProvider 接口。在下面的示例中,MyApplication 类委托度量到 MyInstrumentationProvider 类。用于MyListener 类的代码与用于前一示例中的代码相同。

例 3.5. C#

public class MyApplication : IInstrumentationEventProvider
{
private MyInstrumentationProvider instrumentationProvider =
new MyInstrumentationProvider();

public object GetInstrumentationEventProvider()
{
return instrumentationProvider;
}
}

[InstrumentationListener(typeof (MyListener))]
public class MyInstrumentationProvider
{
[InstrumentationProvider("DbConnect")]
public event EventHandler<EventArgs> OnDbConnect;

public bool IsWired
{
get { return OnDbConnect != null; }
}
}

例 3.6. Visual Basic .NET

Public Class MyApplication Implements IInstrumentationEventProvider 
Private instrumentationProvider As MyInstrumentationProvider = New MyInstrumentationProvider
Public Function GetInstrumentationEventProvider() As Object
Return instrumentationProvider
End Function
End Class

<InstrumentationListener(GetType(MyListener))>
Public Class MyInstrumentationProvider
<InstrumentationProvider("DbConnect")>
Public Event OnDbConnect As EventHandler
Public ReadOnly Property IsWired() As Boolean
Get
Return Not (OnDbConnect Is Nothing)
End Get
End Property
End Class

通常,系统会在 MyApplication类上方的第一行查找[InstrumentationListener] 属性。然而,在这种情况下,不出现此属性。因此,当系统遇到 IInstrumentationEventProvider 接口,它将调用GetInstrumentationEventProvider 方法,查找与方法返回的类型一致的类的属性,以及查找任何[InstrumentationProvider] 属性。

安装度量

在监听器类中的度量由[HasInstallableResources] 属性标记,下列示例展示了与一个监听程序类关联的元数据。

例 3.7. C#

[HasInstallableResources]
[PerformanceCountersDefinition("Enterprise Library Data Counters", "CounterCategoryHelpResourceName")]
[EventLogDefinition("Application", "Enterprise Library Data")]
public class DataInstrumentationListener : InstrumentationListener
{
[PerformanceCounter("Connections Opened/sec", "ConnectionOpenedCounterHelpResource", PerformanceCounterType.RateOfCountsPerSecond32)]
EnterpriseLibraryPerformanceCounter connectionOpenedCounter;
}

例 3.8. Visual Basic .NET

<HasInstallableResources()> _ 
<PerformanceCountersDefinition("Enterprise Library Data Counters", "CounterCategoryHelpResourceName")> _
<EventLogDefinition("Application", "Enterprise Library Data")> _
Public Class DataInstrumentationListener Inherits InstrumentationListener
<PerformanceCounter("Connections Opened/sec", "ConnectionOpenedCounterHelpResource", PerformanceCounterType.RateOfCountsPerSecond32)> _
Private connectionOpenedCounter As EnterpriseLibraryPerformanceCounter
End Class

每种度量的定义指派计数器到一个分类,并为帮助系统指定给它一个帮助资源名。在这有可选的第三个参数指定是否是单一的分类实例或多个实例。一个计数器仅属于一个分类。

InstallUtil.exe 工具为 [HasInstallableResources]属性搜索每个程序集,并指派计数器到正确的分类。运行 InstallServices.bat 批处理文件将调用 InstallUtil.exe 工具并安装所有的度量。也可以使用它来卸载所有的度量。安装度量需要管理员权限。

如果度量被关闭,通过配置或者省略相关的配置节,就可以使用 xcopy 命令来部署应用程序。

posted @ 2007-10-04 00:35  Dorian Deng  阅读(883)  评论(3编辑  收藏  举报