Naming multi-instance performance counters in .NET
- by Roger Lipscombe
Most multiple instance performance counters in Windows seem to automatically(?) have a #n on the end if there's more than one instance with the same name.
For example: if, in Perfmon, you look under the Process category, you'll see:
...
dwm
explorer
explorer#1
...
I have two explorer.exe processes, so the second counter has #1 appended to its name.
When I attempt to do this in a .NET application:
I can create the category, and register the instance (using the PerformanceCounterCategory.Create that takes a CounterCreationDataCollection).
I can open the counter for write and write to it.
When I open the counter a second time, it opens the same counter. This means that I have two applications fighting over the counters.
The documentation for PerformanceCounter.InstanceName states that # is not allowed in the name.
So: how do I have multiple-instance performance counters that are actually multiple instance? And where the second (and subsequent) instances get #n appended to the name?
That is: I know that I can put the process ID (e.g.) on the instance name. This works, but has the unfortunate side effect that restarting the process results in a new PID, and Perfmon continues monitoring the old counter.