Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# PerformanceCounter list of possible Parameters?

I am using the System.Data PerfomanceCounter class, and I have found some very specific examples of how to retrieve disk space or CPU usage.

However I am unable to find the documentation on the possible values for:

PerfomanceCounter.CategoryName
PerformanceCounter.CounterName
PerformanceCounter.InstanceName

I can see from the class the different parameters I can pass to the constructor, but even going to the page for say CategoryName does not give me a list of available values.

Can anyone offer and advice on how to find available values for these?

Thanks!

like image 771
sec_goat Avatar asked Apr 29 '14 13:04

sec_goat


3 Answers

Those can be obtained from the PerformanceCounterCategory class:

PerformanceCounterCategory[] categories = PerformanceCounterCategory.GetCategories();
foreach(var category in categories)
{
    string[] instanceNames = category.GetInstanceNames();
    foreach(string instanceName in instanceNames)
        PerformanceCounter[] counters = category.GetCounters(instanceName);
}
like image 115
D Stanley Avatar answered Oct 06 '22 11:10

D Stanley


Here is some code that can be modified to get the counters that you desire(there are way too many to post).

All Categories will be outputted but the counters will only be outputted for the desired categories.

    private static void PrintPerformanceCounterParameters()
    {
        var sb = new StringBuilder();
        PerformanceCounterCategory[] categories = PerformanceCounterCategory.GetCategories();

        var desiredCategories = new HashSet<string> {"Process", "Memory"};

        foreach (var category in categories)
        {
            sb.AppendLine("Category: " + category.CategoryName);
            if (desiredCategories.Contains(category.CategoryName))
            {
                PerformanceCounter[] counters;
                try
                {
                    counters = category.GetCounters("devenv");
                }
                catch (Exception)
                {
                    counters = category.GetCounters();
                }

                foreach (var counter in counters)
                {
                    sb.AppendLine(counter.CounterName + ": " + counter.CounterHelp);
                }
            }
        }
        File.WriteAllText(@"C:\performanceCounters.txt", sb.ToString());
    }
like image 22
tjsmith Avatar answered Oct 06 '22 11:10

tjsmith


You can find them on the Microsoft page by searching the category + "object". E.g., for the memory process objects, it would be like this: "performance counters process object". https://msdn.microsoft.com/en-us/library/ms804621.aspx

like image 1
Luc Avatar answered Oct 06 '22 10:10

Luc