Listing all performance counters on Windows with C# .NET
August 1, 2017 Leave a comment
Performance counters in Windows can help you with finding bottlenecks in your application. There’s a long range of built-in performance counters in Windows which you can view in the Performance Monitor window:
Right-click anywhere on the larger screen to the right and select Add Counters to add your counters to the graph. The Add Counters window will show the categories first. You can then open a category and select one or more specific counters within that category. The graph will show the real-time data immediately:
The System.Diagnostics namespace has a couple of objects that let you find the available performance categories and counters on the local machine or on another machine. Each performance category has a name, a help text and a type. It’s straightforward to find the categories available on a machine:
PerformanceCounterCategory[] categories = PerformanceCounterCategory.GetCategories(); foreach (PerformanceCounterCategory category in categories) { Console.WriteLine("Category name: {0}", category.CategoryName); Console.WriteLine("Category type: {0}", category.CategoryType); Console.WriteLine("Category help: {0}", category.CategoryHelp); }
GetCategories() has an overload where you can specify a computer name if you’d like to view the counters on another computer within the network.
At the time of writing this post I had 161 categories on my machine. Example:
Name: WMI Objects
Help: Number of WMI High Performance provider returned by WMI Adapter
Type: SingleInstance
Once you got hold of a category you can easily list the counters within it. The below code prints the category name, type and help text along with any instance names. If there are separate instances within the category then we need to called the GetCounters method with the instance name if it exists otherwise we’ll get an exception saying that there are multiple instances.
PerformanceCounterCategory[] categories = PerformanceCounterCategory.GetCategories(); foreach (PerformanceCounterCategory category in categories) { Console.WriteLine("Category name: {0}", category.CategoryName); Console.WriteLine("Category type: {0}", category.CategoryType); Console.WriteLine("Category help: {0}", category.CategoryHelp); string[] instances = category.GetInstanceNames(); if (instances.Any()) { foreach (string instance in instances) { if (category.InstanceExists(instance)) { PerformanceCounter[] countersOfCategory = category.GetCounters(instance); foreach (PerformanceCounter pc in countersOfCategory) { Console.WriteLine("Category: {0}, instance: {1}, counter: {2}", pc.CategoryName, instance, pc.CounterName); } } } } else { PerformanceCounter[] countersOfCategory = category.GetCounters(); foreach (PerformanceCounter pc in countersOfCategory) { Console.WriteLine("Category: {0}, counter: {1}", pc.CategoryName, pc.CounterName); } } }
Each counter in turn has a name, a help text and a type. E.g. here’s a counter with the “Active Server Pages” category:
Name: Requests Failed Total
Help: The total number of requests failed due to errors, authorization failure, and rejections.
Type: NumberOfItems32
You can view all posts related to Diagnostics here.