System.Diagnostics namespace provides classes which we can use to create and manipulate custom performance counters. Given below are the steps which we typically use for this purpose.

Create a collection of type CounterCreationDataCollection. Create the counters you want to create as objects of type CounterCreationData and set their necessary properties. Add the CounterCreationData objects to the collection by calling the collection's Add method. Call the Create method on the PerformanceCounterCategory class and pass the collection to it.

'Assumes import statement for System.Diagnostics.dllDim CounterDatas As New System.Diagnostics.CounterCreationDataCollection()

'Create the counters and set their properties.Dim cdCounter1 As New System.Diagnostics.CounterCreationData()Dim cdCounter2 As New System.Diagnostics.CounterCreationData()

'Add both counters to the collection.CounterDatas.Add(cdCounter1)CounterDatas.Add(cdCounter2)

'Create the category and pass the collection to it.PerformanceCounterCategory.Create("Accounting", _ "A set of counters for the accounting application", CounterDatas)

If you need to create a category with just one performance counter, you can use the overload of the Create method as follows:

PerformanceCounterCategory.Create("Accounting", _ "A set of counters for the accounting application", _ "Transactions Active","Gives number of transactions being processed")

There are a few things to consider regarding custom performance counters.

You cannot create custom categories and counters on remote machines. Our interaction with custom counters and categories is restricted to read-only mode unless you explicitly specify otherwise. By default the counters created under server explorer are read only and under read-only mode you cannot update the value of the counter. To create custom counters on the local computer you need administrative access. You cannot create new counters within existing custom categories. If you need to add counters to categories that already exist, the only way you can do so is to delete the category and recreate it with all of its contents, including the new counters you want to add. If you try to create a counter that already exists, an error would be thrown. You can check the existence of a counter before you create one. For example:

If Not (PerformanceCounterCategory.Exists("MyCat")) Then PerformanceCounterCategory.Create("MyCat", "Desc", "MyCtr", "Desc")End If

Feedbacks

Post Comment:

Notify me by email when others post comments to this article.

Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.