7

I have this piece of code : Where I create my Performance Counter. It executes ok, if not exists it creates the performance counter as well, but I can't find this performance counter, when I use perfmon.

What is 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);
    }
Bryan
  • 11,398
  • 3
  • 53
  • 78
  • 2
    One issue I have run into with perf counters in the past is that the running process has to be an administrator, or have certain permissions to create perf counters. This is why typically new perf counters are created at install-time instead of run-time. I don't remember what happens if your app doesn't have admin rights though; it might just silently fail to create the counters. Though I would think it would throw an exception... but anyway, try just running your app as admin, if you aren't already. – CodingWithSpike Jul 17 '12 at 18:51
  • 3
    Also, if you create the counters while perfmon is running, you need to restart perfmon to make it recognize the new counters. – Aasmund Eldhuset Jul 17 '12 at 18:58
  • Plus, counters are not visible immediately. Sometimes it takes seconds to be able to see them. – Wiktor Zychla Jul 17 '12 at 18:59
  • So, it is possible to `PerformanceCounterCategory.Create()` do nothing and do not throw an exception. – Guilherme de Jesus Santos Jul 17 '12 at 19:03
  • I just ran you code on windows 7 and the category was created as expected. And you're not really creating a [PerformanceCounter](http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.aspx), you are just defining a PerformanceCounterCategory. Try creating a PerformanceCounter to find out if an exception is thrown. – Thomas C. G. de Vilhena Sep 14 '12 at 23:57

2 Answers2

2

The reason for not receiving any exceptions is try-catch block is missing. If you add your statements in 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 capture the exceptions.If you see exception like "Requested registry access is not allowed." then you require administrative rights to do the stuff. To confirm this Run the Visual Studio with Administrative rights and execute the code.

Vishal
  • 606
  • 5
  • 5
1

Aside from running Visual Studio as Administrator to allow the creation of the categories, I had the same issue - .NET code reported that the counters were there, but there were no such counter categories visible in perfmon.

Apparently perfmon will sometimes disable performance counters by flagging it as disabled in the registry.

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

Community
  • 1
  • 1
SharpC
  • 6,974
  • 4
  • 45
  • 40