I have just adding some Performance Counters to instrument some code and had a few issues that are worth knowing about.
I created two categories of counters using the following code:
//Create a category with a single counter
PerformanceCounterCategory.Create(
“categoryName”, “categoryDescription”,
PerformanceCounterCategoryType.SingleInstance,
“counterName”, “counterDescription”);
//Create a category with more than one counter
System.Diagnostics.CounterCreationDataCollection counterCollection System.Diagnostics.CounterCreationDataCollection();
counterCollection.Add(new System.Diagnostics.CounterCreationData(
“Name1,
“Description1”,
PerformanceCounterType.NumberOfItems64));
counterCollection.Add(new System.Diagnostics.CounterCreationData(
“Name2,
“Description2”,
PerformanceCounterType.NumberOfItems64));
// Create the category and pass the collection to it.
System.Diagnostics.PerformanceCounterCategory.Create(
“multicategoryName”,
“multicategoryDescription”,
PerformanceCounterCategoryType.SingleInstance,
counterCollection);
The problem I had was the new performance counters did not show up correctly in PerfMon, but seemed OK from Visual Studio. In PerfMon I was seeing the single counter category OK but the multi counter category was only showing the first counter.
If I sent data to one of the multi category counters it seemed to go OK but in Perfmon I saw nothing. If I sent to the single category counter it showed up on both the single and multi graph in PerfMon.
The fix was simple, unload PerfMon completely between tests and all is OK. Just loading the Add Counter dialog IS NOT ENOUGH.
Also it is worth calling
System.Diagnostics.PerformanceCounter.CloseSharedResources();
when adding or deleting categories as this removes stale data that can also be a problem.