Where is my performance counter? It is created, but I do not see it in perfmon

I have this piece of code: Where I create a performance counter. It runs fine, if it does not exist, it also creates a performance counter, but I cannot find this performance counter when I use perfmon.

What's happening?

const string _categoryName = "MyPerformanceCounter"; if (!PerformanceCounterCategory.Exists(_categoryName)) { CounterCreationDataCollection counters = new CounterCreationDataCollection(); CounterCreationData ccdWorkingThreads = new CounterCreationData(); ccdWorkingThreads.CounterName = "# working threads"; ccdWorkingThreads.CounterHelp = "Total number of operations executed"; ccdWorkingThreads.CounterType = PerformanceCounterType.NumberOfItems32; counters.Add(ccdWorkingThreads); // create new category with the counters above PerformanceCounterCategory.Create(_categoryName, "Performance counters of my app", PerformanceCounterCategoryType.SingleInstance, counters); } 
+7
source share
2 answers

The reason for not getting any exceptions is the try-catch block. If you add your statements in a try and catch block, like this

  try { const string _categoryName = "MyPerformanceCounter"; if (!PerformanceCounterCategory.Exists(_categoryName)) { CounterCreationDataCollection counters = new CounterCreationDataCollection(); CounterCreationData ccdWorkingThreads = new CounterCreationData(); ccdWorkingThreads.CounterName = "# working threads"; ccdWorkingThreads.CounterHelp = "Total number of operations executed"; ccdWorkingThreads.CounterType = PerformanceCounterType.NumberOfItems32; counters.Add(ccdWorkingThreads); // create new category with the counters above PerformanceCounterCategory.Create(_categoryName, "Performance counters of my app", PerformanceCounterCategoryType.SingleInstance, counters); } } catch(Exception ex) { MessageBox.Show(ex.ToString()); //Do necessary action } 

Then it will catch exceptions. If you see an exception like "Requested registry access is not allowed." then you need administrative rights to do this. To confirm this, run Visual Studio as an administrator and execute the code.

+2
source

In addition to running Visual Studio as an administrator in order to allow the creation of categories, I had the same problem - in the .NET code it was reported that the counters were there, but there were no such categories of counters visible in perfmon.

Apparently, perfmon sometimes disables performance counters, putting it as disabled in the registry .

If you check in the registry under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services , you can find your category of performance counters (just find your category name as one of the "folders"). Under the Performance unit ("folder"), find the Disable Performance Counters registry value and set it to zero. Restart perfmon, and now you should see your categories and counters in perfmon.

+1
source

All Articles