After spending a couple of days looking at how the instrumentation works in the Enterprise Library and finding out how lacking the documentation is around instrumentation I thought I would share my experiences with you.  The best I dive into solution mode I thought I would describe what the enterprise library provides on top of the out of the box performance counters. The biggest thing it does is abstract away the creation and management of the performance counter.   Instead of writing code you can just use a few of attributes, use the EnterpriseLibraryPerformanceCounter class, create an installer and away you go.   It is that simple.  

Rather than leave it at that, let me walk you through the exact steps that you need to follow.

Step 1. Create a Class to House the Performance Counters

The first thing that you need to do create a class that will hold the performance counters, and do the work of updating them when required.  For example: 

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation;
using System.Diagnostics;
namespace SamplePerformanceCounter
{
    
    [HasInstallableResources]
    [PerformanceCountersDefinition(PerformanceCategory, "SampleInstrumentationCounterHelp")]
    public class SimplestPerformanceClass
    {
        private const string PerformanceCategory = "Simplest Performance Category";
        
        [PerformanceCounter("Operations Started/sec", "OperationStartedHelpResource", PerformanceCounterType.RateOfCountsPerSecond32)]
        private EnterpriseLibraryPerformanceCounter _operationStarted = new EnterpriseLibraryPerformanceCounter(PerformanceCategory, "Operations Started/sec");
        public void OperationStarted()
        {
            _operationStarted.Increment();
        }
    }
}

Step 2. Create an Project Installer

The next step is to create a project installer, this is really really simple. Just create a class that looks like the following.

using System.ComponentModel;
using System.Configuration.Install;
using System.Management.Instrumentation;
using Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation;
namespace SamplePerformanceCounter
{
    /// <summary>
    /// Let the system know that the InstallUtil.exe tool will be run against this assembly
    /// </summary>
    [RunInstaller(true)]
    public partial class ProjectInstaller : DefaultManagementProjectInstaller
    {
        /// <summary>
        /// Represents the installer for the instrumentation events. Not intended for direct use.
        /// </summary>
        public ProjectInstaller()
        {
            Installers.Add(new ReflectionInstaller<PerformanceCounterInstallerBuilder>());
        }
    }
}

Step 3. Use the Performance Counter Class

All the hard work is done, you can now start to use the performance counters.  For example

SimplestPerformanceClass performance = new SimplestPerformanceClass();
for (int i = 0; i < 1000000; i++)
{
     performance.OperationStarted();
}

Step 4. Install the Performance Counters

The final setp is to install the performance counters.  This is done simply by running install util over the assembly that contains the project installer.  This is as simple as and looking for “The Commit Phase Completed Successfully”

> intallutil simplestpossibleperformancecounter.exe

You’re done.   To see the output of all your hard work fire up performance monitor, add in the your counter to the output and away you go!

 

In my next post I’ll describe how instrumentation implement instrumentation, in the same way as the Application blocks in the Enterprise Library do.   

Advertisements