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. 

<configuration>

  <configSections>

    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

    <section name="instrumentationConfiguration"type="Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.Configuration.InstrumentationConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

  </configSections>

  <instrumentationConfiguration performanceCountersEnabled="true"

    eventLoggingEnabled="false" wmiEnabled="false" applicationInstanceName="" />

</configuration>

 

 

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)
{
     Console.WriteLine(i);
}

 

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

 

Note:

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

http://msdn.microsoft.com/en-us/library/ff648494.aspx

 

 

 

C#

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.

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();
        }

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.

 

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.

 

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.

C#