Creating a new performance counter on Windows with C# .NET
October 15, 2017 Leave a comment
In this post we saw how to list all performance counter categories and the performance counters within each category available on Windows. The last time I checked there were a little more than 27500 counters and 148 categories available on my local PC. That’s quite a lot and will probably cover most diagnostic needs where performance counters are involved in a bottleneck investigation.
However, at times you might want to create your own performance counter. The System.Diagnostics library provides the necessary objects. Here’s how you can create a new performance counter category and a counter within that category:
string categoryName = "Football";
if (!PerformanceCounterCategory.Exists(categoryName))
{
string firstCounterName = "Goals scored";
string firstCounterHelp = "Goals scored live update";
string categoryHelp = "Football related real time statistics";
PerformanceCounterCategory customCategory = new PerformanceCounterCategory(categoryName);
PerformanceCounterCategory.Create(categoryName, categoryHelp, PerformanceCounterCategoryType.SingleInstance, firstCounterName, firstCounterHelp);
}
