Attribute based Performance Counters with Enterprise Library 4.1

This post is based on my previous post on Getting started with Enterprise Library Performance Counters

Let’s assume you don’t want to use the config files to configure performance counters. You need to use the PerformanceCounterCallHandlers.

In order to achieve this, you need to follow the same steps – until step number 7, which I have stated in my previous post.

Step number 8 is different. Your config file will only contain the below.

    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <section name="instrumentationConfiguration"type="Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.Configuration.InstrumentationConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  <instrumentationConfiguration performanceCountersEnabled="true"
    eventLoggingEnabled="false" wmiEnabled="false" applicationInstanceName="" />

As you can see above, you can easily turn off the performance counters as a whole entity.

Now to add performance counters to a target method, you can simply use the PerformanceCounterCallHandler attribute as specified below.

[PerformanceCounterCallHandler("DemoCounters", "Operations Started/sec", IncrementCallsPerSecond = true)]
public void SampleMethod(int i)