Raj Aththanayake's Blog Raj Aththanayake's Blog | August 2010

Attribute based Performance Counters with Enterprise Library 4.1

26. August 2010


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)


Now you should see the similar results in Windows Performance Monitor as I stated in my previous post.



If you need to know more in PerformanceCounterCallHandler please refer to the below link






Getting started with Enterprise Library Performance Counters

21. August 2010

This article is based on Enterprise Library 4.1 - October 2008.

One of the hurdles of using Enterprise Library is that there are no much  help on how to do certain things from the scratch. The Enterprise Library  Quick Start guide and the Labs are helpful but I find there is no much depth in some areas such as Performance counters. Recently I had to utilize some performance counters within our application.  It wasn’t that straightforward and I had to do lot of trial and error exercises to get it working. This is an attempt to give you some insight into how to utilise performance counters using Enterprise Library 4.1.

 I’m going to explain you how to create performance counters using Entib configuration. There are other ways - For example using Performance Counter handlers using attributes which I’m not going to explain here. Also I do not cover detail information on using performance counters and best practices. This is just to get you started with Entlib 4.1 and performance counters

First thing is first – You must install the Microsoft practices Enterprise Library 4.1 from the location here.


Creating performance counters using Entlib configuration.

1.       To create counters you need to have an administrative account. You need to open  Visual Studio (VS) as an administrator. Then create a new Console application for example let’s say it SamplePerformanceCounters.

Now add below reference to your project. 


      2.       Create a new class called MyCounters.cs.

Your class should derive from MarshalByRefObject. This class sits under System namespace and you need to add the using statement using System.Reflection;

             public class MyCounters : MarshalByRefObject {}

MarshalByRefObject object will allow you to do policy injection on the MyCounters class. Alternatively you may also create an interface i.e IMyCounters. This will allow us to create a proxy object that can be used with Entlib policy injection.

              public void SampleMethod(int i) { }


4.       Our next task is to install the counters which we want to execute on SampleMethod. You can create performance counters as below.


        public static void  InstallCounters()
            PerformanceCountersInstaller installer = new PerformanceCountersInstaller(new SystemConfigurationSource());
            IDictionary state = new System.Collections.Hashtable();
            installer.Context = new InstallContext();
            Console.WriteLine("Performance counters have been successfully installed. Press enter to continue");

These counters needed to be installed first before you calling the SampleMethod. Therefore within your Main method call the InstallCounters() method.

Note : You can also remove the installed counters by....

        private static void RemoveCounters()
            PerformanceCountersInstaller installer  = new PerformanceCountersInstaller(new SystemConfigurationSource());
            installer.Context = new InstallContext();
            Console.WriteLine("Performance counters have been successfully removed. Press enter to continue.");

5.       Within the Main method you need to create the policy injection for MyCounters object.

    MyCounters myCounter = PolicyInjection.Create<MyCounters>();


6.       Now you can call the SampleMethod as below. You can create a for loop and pass the integer variable to the sample method so every time when it executes, you can see the integer in the console window.



7.       All together this is how it looks like



         static void Main(string[] args)

            MyCounters myCounter = PolicyInjection.Create();

            for (int i = 0; i < 100000000; i++)





      Note that we use Thread.Sleep so we can delay the method execution, which gives us clear indications on the performance being monitored.


We have not finished yet. Below is the most important section.


8.       Now we need to define the counters that we need to instrument. To do that you need to create an App.config file. You can either add entries to app.config manually, or you could use the Enterprise library configuration tool which comes with the installation. My personal opinion is to use the tool as it is much easier to configure the counters.


9.       You also need to create a policy injection application block and a policy with a matching rule. In our case I created the SampleCounters with the Method Signature matching rule.



10.       The final step is to configure the counters.



That’s it, and run your application.  

To view the performance counters in action, you need to open the Windows Performance Monitor tool. You can find it in Windows Administrative tools.

As mentioned above Press enter to continue. While the integers are printing in the console window, click on Add Counters and select Demo Counter category from the list. Add Instances as required. Click OK. Now you should be able to see counters in action in the Windows Performance Monitor.


I hope this helps.