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);
}
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With