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 aCounterCreationDataCollection
). - I can open the counter for write and write to it.
When I open the counter a second time (in another process), it opens the same counter. This means that I have two processes 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.
Update:
I'm creating the category (and counter) as follows:
const string categoryName = "Test App";
const string counterName = "Number of kittens";
string instanceName =
Path.GetFileNameWithoutExtension(
Process.GetCurrentProcess().MainModule.FileName);
if (!PerformanceCounterCategory.Exists(categoryName))
{
var counterCreationDataCollection = new CounterCreationDataCollection
{
new CounterCreationData(counterName, "",
PerformanceCounterType.NumberOfItems32)
};
PerformanceCounterCategory.Create(categoryName, "",
PerformanceCounterCategoryType.MultiInstance,
counterCreationDataCollection);
}
I'm opening the counter as follows:
PerformanceCounter counter = new PerformanceCounter(
categoryName, counterName, instanceName, readOnly: false);