سؤال

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!

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top