BlackWaspTM
System Information
.NET 1.1+

Setting Performance Counters

Microsoft Windows provides a large number of performance counters that can be monitored with tools such as Performance Monitor. It is possible to add new counters and set their values to allow such tools to monitor custom .NET applications and services.

Performance Counters

Microsoft Windows provides a number of counters that can be used to determine how well the operating system, hardware, services and drivers are performing. You can also create your own performance counters and categories and monitor these using tools such as Performance Monitor. This can be useful in determining the correct operation of your software and to assist in identifying performance bottlenecks, bugs or memory leaks.

Creating a New Performance Counter

In this article we will a new single-instance performance counter and a sample application that allows the counter's value to be changed. We will view the changes with Performance Monitor. We will create the counter manually using the tools provided by Visual Studio. You can also create performance counters programmatically, though this will not be examined here. To begin, open Visual Studio as an administrative user. This is required to allow a performance counter to be added.

Performance counter categories and their contained counters can be viewed in the Server Explorer pane. To view this, select "Server Explorer" from the View menu. Within the Server Explorer, expand the Servers node and the name of your computer. You should see a branch of the tree named, "Performance Counters". If you expand this section you will see a list of all of the categories that are present on the computer. Within each of these nodes will be the names of the contained counters.

To create a new performance counter category, right-click the Performance Counters tree node. Select "Create New Category..." from the context-sensitive menu that appears. This displays the Performance Counter Builder dialog box, pictured below:

Creating a Performance Counter

The image above shows the performance counter that we will now create. First, enter "Custom Counters" as the category name. This would normally contain the name of the application or service that you wish to be monitored. Add a suitable description for the category.

To add a counter to the category, click the New button. You can specify a name, type and description for each counter that you include in the category. For this demonstration, name the counter, "Example Counter". The counter will present a single integer value so select "NumberOfItems32" as the type. Add a description and click the OK button to create the category and counter. You should be able to see the new items in the Server Explorer.

Viewing the Performance Counter

To monitor the counter, launch the Performance Monitor software and add the new counter to the chart. You should see that the new chart line is constantly set to zero, as no value has been provided yet. Leave the performance monitor running.

3 August 2010