C# PerformanceCounter list of possible Parameters?

谁说胖子不能爱 提交于 2019-12-10 08:48:07

问题


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!


回答1:


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);
}



回答2:


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());
    }



回答3:


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



来源:https://stackoverflow.com/questions/23366831/c-sharp-performancecounter-list-of-possible-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!