Can not read custom PerformanceCounter Instances

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am implimenting Performance counters into a web application.

I use the following code to create the counters during setup:
private void SetupPerfCntrs()
{
System.Diagnostics.CounterCreationDataCollection CounterDatas = null;
System.Diagnostics.CounterCreationData cdCounter4 = null;
try
{
if(System.Diagnostics.PerformanceCounterCategory.Exists("LogisOnline"))
{
System.Diagnostics.PerformanceCounterCategory.Delete("LogisOnline");
}
// Create a collection of type CounterCreationDataCollection.
CounterDatas = new System.Diagnostics.CounterCreationDataCollection();
// Create the counters and set their properties.
cdCounter4 = new System.Diagnostics.CounterCreationData();
cdCounter4.CounterName = "PaymentsView Response";
cdCounter4.CounterHelp = "Time in millisecods for Payment View call to
respond";
cdCounter4.CounterType =
System.Diagnostics.PerformanceCounterType.NumberOfItems32;
// Add both counters to the collection.
CounterDatas.Add(cdCounter4);
// Create the category and pass the collection to it.
System.Diagnostics.PerformanceCounterCategory.Create("LogisOnline", "Logis
Online Web Application Metrics", CounterDatas);
}
catch(Exception ex)
{
throw ex;
}
finally
{
CounterDatas = null;
cdCounter4 = null;
}
}

The code used to write to the performance counters are as follows:
Dim PCPaymentView As System.Diagnostics.PerformanceCounter = New
System.Diagnostics.PerformanceCounter("LogisOnline", "PaymentsView Response",
"_Total", False)
Dim StartTime As DateTime = DateTime.Now
 
I found the problem. A Counter becomes a single instance counter if you write
to it the first time as a single instance.

Using the following constructor creates a single instance counter:
PerformanceCounter pc = new PerformanceCounter("Catagory", "Counter", false)

After you have done this, setting InstanceNames and calling RawValue has no
effect.

To create multi instance counters always use the Constructor with Instamce
Names as below:
PerformanceCounter pc = new PerformanceCounter("Catagory", "Counter",
"_Total", false)

After this, any number of instances can be created as required and
documented everywhere by setting the instance name and setting a rawvalue;

In my case, I was attemting to add instances to counters that have already
been created and where being used by other Performance Counter Instances in
the system.

David B. Taylor
 
Back
Top