Getting started with Enterprise Library Performance Counters

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.
http://www.microsoft.com/downloads/details.aspx?FamilyId=1643758B-2986-47F7-B529-3E41584B6CE5&displaylang=en

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.
Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.Installers;

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();
    installer.Install(state);
    installer.Commit(state);
    Console.WriteLine("Performance counters have been successfully installed. Press enter to continue");
    Console.ReadLine();
}

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();
    installer.Uninstall(null);
    Console.WriteLine("Performance counters have been successfully removed. Press enter to continue.");
    Console.ReadLine();
}

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();
    installer.Uninstall(null);
    Console.WriteLine("Performance counters have been successfully removed. Press enter to continue.");
    Console.ReadLine();
}

5. Within the Main method you need to create the policy injection for MyCounters object.
MyCounters myCounter = PolicyInjection.Create();

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.
myCounter.SampleMethod(i)

7. All together this is how it looks like

static void Main(string[] args)
{
    InstallCounters();
    //RemoveCounters();    

    MyCounters myCounter = PolicyInjection.Create();

    for (int i = 0; i < 100000000; i++)
    {
            Thread.Sleep(500);
            service.SampleMethod(i);
    }
    Console.ReadLine();
}

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.

image1

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.

image2

10. The final step is to configure the counters.
image3

That’s it, and run your application.
image4

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.

image5

I hope this helps.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s