Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all performance counters for a category

Is there a built-in method in System.Diagnostics for retrieving all of the instantiated performance counters for a given CategoryName?

We have a number of multi threaded apps using custom performance counters and now need to add a dashboard for displaying the performance statistics.

I'd like to make the dashboard in such a way that it does not need to be updated whenever someone adds a new counter to a new piece of code.

like image 802
grenade Avatar asked Aug 20 '09 09:08

grenade


People also ask

Which are performance counters?

Performance counters are bits of code that monitor, count, or measure events in software, which allow us to see patterns from a high-level view. They are registered with the operating system during installation of the software, allowing anyone with the proper permissions to view them.

What are performance counters Windows?

Windows Performance Counters provide a high-level abstraction layer that provides a consistent interface for collecting various kinds of system data such as CPU, memory, and disk usage. System administrators often use performance counters to monitor systems for performance or behavior problems.


1 Answers

Try this:

public void ListCounters(string categoryName)
{
    PerformanceCounterCategory category = PerformanceCounterCategory.GetCategories().First(c => c.CategoryName == categoryName);
    Console.WriteLine("{0} [{1}]", category.CategoryName, category.CategoryType);

    string[] instanceNames = category.GetInstanceNames();

    if (instanceNames.Length > 0)
    {
        // MultiInstance categories
        foreach (string instanceName in instanceNames)
        {
            ListInstances(category, instanceName);
        }
    }
    else
    {
        // SingleInstance categories
        ListInstances(category, string.Empty);
    }
}

private static void ListInstances(PerformanceCounterCategory category, string instanceName)
{
    Console.WriteLine("    {0}", instanceName);
    PerformanceCounter[] counters = category.GetCounters(instanceName);

    foreach (PerformanceCounter counter in counters)
    {
        Console.WriteLine("        {0}", counter.CounterName);
    }
}

You have to be aware of categories that can have multiple instances and deal with those slightly differently.

like image 114
adrianbanks Avatar answered Oct 21 '22 00:10

adrianbanks