Naming multi-instance performance counters in .NET

Posted by Roger Lipscombe on Stack Overflow See other posts from Stack Overflow or by Roger Lipscombe
Published on 2010-06-07T15:28:10Z Indexed on 2010/06/07 15:32 UTC
Read the original article Hit count: 322

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about performancecounter