NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Creating Custom Performance Counters

When you create a new counter, you first create a category, and then specify one or more counters to be placed in it. You can do this in the following way:

Note   You can also use the CreateCategory method to create a single counter within a new category. For instructions, see Creating Performance Counter Categories.

It is important to note the difference between creating a counter and creating an instance of the PerformanceCounter component. When you create a counter, you are creating a new category and its associated counters in the Windows operating system, rather than a component in your project or application. When you create an instance of the PerformanceCounter class, you create a component that references an external counter.

To create a new category and set of performance counters programmatically

  1. Create an array of CounterCreationData objects, defining all of the counters you want to create within it.
  2. Call the CreateCategory method on the PerformanceCounter class and pass the array of CounterCreationData objects to it.

    The following example shows how to create a series of counters as an array and pass it to the category when you create it:

    [Visual Basic]
       dim cdCounter1 As New CounterCreationData
       dim cdCounter2 As New CounterCreationData
       dim CounterDatas(2) as New CounterCreationData
       cdCounter1.CounterName = strCountName
       cdCounter1.CounterHelp = strCountName & " help string"
       cdCounter1.CounterType = NumberOfItems64
       assign CounterDatas(0) = cdCounter1
       cdCounter2.CounterName = strCountName
       cdCounter2.CounterHelp = strCountName & " help string"
       cdCounter2.CounterType = NumberOfItems64
       assign CounterDatas(1) = cdCounter2
       dim pcTmp as PerformanceCounter
       pcTmp.CreateCategory "Multi Counter Category", "Category help", CounterDatas
    [C#]
       CounterCreationData cdCounter1 = new CounterCreationData();
       CounterCreationData cdCounter2 = new CounterCreationData();
       CounterCreationData[] CounterDatas = new CounterCreationData[2];
       cdCounter1.CounterName = strCountName;
       cdCounter1.CounterHelp = strCountName + " help string";
       cdCounter1.CounterType = NumberOfItems64;
       CounterDatas[0] = cdCounter1;
       cdCounter2.CounterName = strCountName;
       cdCounter2.CounterHelp = strCountName + " help string";
       cdCounter2.CounterType = NumberOfItems64;
       CounterDatas(1) = cdCounter2;
       PerformanceCounter pcTmp = new PerformanceCounter();
       pcTmp.CreateCategory ("Multi Counter Category", "Category help", CounterDatas);

See Also

Creating Performance Counter Categories | Category and Counter Management