Instructions
To add performance counters to your applications will be easier than ever. Now you can give your programs the final improvements that you want without a headache.

how to add to your running application:
  • Add a reference to the assembly PerformanceCounterHelper.dll in your project.
  • Then make a enum within your application that will hold all the performance counters you want. Then use the "PerformanceCounterCategoryAttribute" into the enum type to add needed properties such as category name, category description and category type (single instance or multiple instance)
  • Then within the enum elements add a "PerformanceCounterAttribute" attribute to describe the counter name, counter description, and Counter Type

Here I leave you a enum as an example (you can find a complete example in the source-code section).

    [PerformanceCounterCategoryAttribute("TestApplication", 
    	System.Diagnostics.PerformanceCounterCategoryType.SingleInstance, 
    	"Detail information about the test application.")]
    public enum SingleInstance_PerformanceCounters
    {
        [PerformanceCounterAttribute("CounterExample", 
        	"Example Quantity", 
        	System.Diagnostics.PerformanceCounterType.NumberOfItems32)]
        CounterExample,

        [PerformanceCounterAttribute("AverageCountExample", 
        	"Example Average Counter", 
        	System.Diagnostics.PerformanceCounterType.AverageCount64)]
        AverageCountExample
    }

  • After saving and compiling your application use the intaller given in this solution called "PerformanceCounterHelper.Installer.exe" with the name of the assembly that contains the enum above to install the counters into the system that will run your application (This is done this way because it is possible than the application will run with less privileges than Administrator; and you need to be Administrator to install counters into the system)
  • After that you can start using the performance counters within your applicacion code as follow:

    using (CounterHelper<SingleInstance_PerformanceCounters>  counterHelper = 
              PerformanceHelper.CreateCounterHelper<SingleInstance_PerformanceCounters>())
    {
        counterHelper.Increment(PerformanceCounters.CounterExample)
	counterHelper.Decrement(PerformanceCounters.CounterExample)
    }


    using (CounterHelper<SingleInstance_PerformanceCounters>  counterHelper = 
              PerformanceHelper.CreateCounterHelper<SingleInstance_PerformanceCounters>())
    {
        counterHelper.IncrementBy(PerformanceCounters.AverageCountExample, value);
    }


And then you are done. Start your application and start Perfmon and you will see your counters and will start to play around with them as in the following example:

example.jpg

Last edited Nov 30, 2009 at 3:32 PM by javiercanillas, version 2

Comments

No comments yet.