Chris's profileChris Burrows' Live Spac...PhotosBlogListsMore Tools Help

Blog


    September 08

    The simplest possible way to add performance counters to your application using Enterprise Library January 2006

    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.   

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://madtechnology.spaces.live.com/blog/cns!F637E4FD356E127D!121.trak
    Weblogs that reference this entry
    • None