This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

System Information
.NET 2.0+

Creating Performance Counters Programmatically

Performance counters allow numerical information about the current state of the operating system or an application to be recorded and monitored using standard tools. This article explains how to create custom performance counters using C# code.

Creating Other Performance Counter Types

If you wish to create more than one performance counter within a group, or if you want to create counters of types other than NumberOfItems32, you can use an alternative overloaded version of the Create method. This version of the method requires that you create a CounterCreationDataCollection instance, which holds the details of all counters. You can start by creating an empty counter collection using the default constructor:

CounterCreationDataCollection counters = new CounterCreationDataCollection();

Once you have a collection, you can add a number of counters to it. Each has a name and some descriptive text, as in the previous examples. In addition, you can supply the type of data that the counter will be used to monitor. Each counter is defined within a CounterCreationData object. You can create these individually and configure their properties to define the counter, or you can provide this information to a constructor, as in the example below. This sample code adds two counters to the collection. The first holds the number of orders placed on the current date via a web site. The second counter is used to monitor the rate at which such orders are being received, using the RateOfCountsPerSecond32 counter type.

counters.Add(new CounterCreationData(
    "Today's Web Orders", "Number of Orders", PerformanceCounterType.NumberOfItems32));
counters.Add(new CounterCreationData(
    "Web Order Rate", "Orders per Second", PerformanceCounterType.RateOfCountsPerSecond32));

With the collection created and populated, the overloaded version of the Create method can be executed. As with the first examples, the first three parameters are used to specify the category details. The fourth parameter accepts the counter collection.

    "Web Orders", "Web Site Performance", PerformanceCounterCategoryType.SingleInstance,

With the two counters created, you can set their values in the same manner as before. Note that when the Web Order Rate counter is viewed in Performance Monitor, the rate of change of value is shown. Setting the RawValue property using the code below will cause a peak in the chart, rather than setting a steady value.

PerformanceCounter count = new PerformanceCounter(
    "Web Orders", "Today's Web Orders", false);
count.RawValue = 253912;

PerformanceCounter rate = new PerformanceCounter(
    "Web Orders", "Web Order Rate", false);
rate.RawValue = 10;

Deleting Performance Counter Categories

If you create performance counters and categories during the installation of a product, you should also remove those counters if the user elects to uninstall your software. This is achieved with the Delete method of the PerformanceCounterCategory class. To delete a category, pass its name as the only argument. The category and all of its counters will then be deleted.

To remove all of the counters used in the above samples, execute the following:

PerformanceCounterCategory.Delete("Area Sales");
PerformanceCounterCategory.Delete("Web Orders");
27 October 2010