Implementing Performance Counter in your application

Microsoft Windows has a Performance Counter application that is located in the Administrator Folder.

Performance Counter

Go to Control Panel\All Control Panel Items\Administrative Tools\ and select Performance Monitor. This will open a window with by default the processor performance. What is interesting is that you can see a lot of already coded performance counter by right clicking the list of counter and select Add Counters… or to press the button with the “+” icon.

Some already available .Net counters

What is interesting is that you can create your own inside your application and be able to visualize the result within this Performance Counter.

What you need is the System.Diagnostics namespace. From there you can verify if the counter you want to create exist with the static method ‘PerformanceCounterCategory.Exists’.

To create your counter, you need ‘PerformanceCounterCategory.Create’.

           PerformanceCounterCategory.Create("CategoryName",
                                              "CategoryHelpText",
                                               PerformanceCounterCategoryType.SingleInstance,
                                               "CounterNameInsideTheCategory",
                                               "CounterHelpText."); 

This code must only executed when a verification is made with the Exists method of PerformanceCounterCategory because an exception will be throw.

When you are ready to use the counter you need to use the method Increment or IncrementBy. The first one will increment by one and the other one with the value passed in a parameter. If it requires to decrease, you will need to pass a negative value.

var pc = new PerformanceCounter("CategoryName", "CounterNameInsideTheCategory", false);
pc.IncrementBy(10);

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.