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
"W1ld0ne74" wrote:
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.E xists("LogisOnline"))
{
System.Diagnostics.PerformanceCounterCategory.Dele te("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.NumberOf Items32;
// Add both counters to the collection.
CounterDatas.Add(cdCounter4);
// Create the category and pass the collection to it.
System.Diagnostics.PerformanceCounterCategory.Crea te("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
.
.
.
Dim TimeTaken As Int32 = DateTime.Now.Subtract(StartTime).TotalMilliseconds
Dim StoreNumber As String = "Some Store"
PCPaymentView.RawValue = TimeTaken
PCPaymentView.InstanceName = StoreNumber
PCPaymentView.RawValue = TimeTaken
PCPaymentView.Dispose()
A third application reads the performance counters by using the following
code:
PerformanceCounter pc = null;
try
{
pc = new PerformanceCounter("LogisOnline","PaymentsView Response","_Total");
Messagebox.show(pc.RawValue)
}
All works well except for the final peice of code which throws an exception
stating the the counter is of single instance type. Actual Exception is as
follows:
Counter is single instance, instance name '_Total' is not valid for this
counter category.
Any help would be VERY MUCH Apreciated
Thanks
David Taylor