Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are my Performance Counter? It is created but I can't see it in perfmon

Tags:

c#

.net

perfmon

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);
    }
like image 222
Guilherme de Jesus Santos Avatar asked Jul 17 '12 18:07

Guilherme de Jesus Santos


People also ask

How do I add a performance counter in perfmon?

In the navigation pane, expand Monitoring Tools, and then choose Performance Monitor. In the console pane toolbar, choose the Add button. In the Add Counters window, in the Select counters from computer drop-down list, choose the computer that is running Business Central Server.

How do I open a Performance Monitor file?

The Performance Monitor Properties page opens at the Source tab. In the Data Source section, select Log files, and then choose the Add button. Browse to the log file that you want to view, and then choose the Open button. Choose the OK button.


2 Answers

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.

like image 152
Vishal Avatar answered Sep 28 '22 00:09

Vishal


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.

like image 22
SharpC Avatar answered Sep 27 '22 23:09

SharpC