C#PerformanceCounter可能的参数列表?

时间:2014-04-29 13:59:09

标签: c# parameters performancecounter

我正在使用System.Data PerfomanceCounter类,我找到了一些非常具体的例子来说明如何检索磁盘空间或CPU使用情况。

但是我无法找到有关可能值的文档:

PerfomanceCounter.CategoryName
PerformanceCounter.CounterName
PerformanceCounter.InstanceName

I can see from the class the different parameters我可以传递给构造函数,但即使转到the page for say CategoryName也没有给我一个可用值列表。

有人可以提供有关如何找到这些值的可用值的建议吗?

谢谢!

3 个答案:

答案 0 :(得分:6)

可以从PerformanceCounterCategory类获得:

PerformanceCounterCategory[] categories = PerformanceCounterCategory.GetCategories();
foreach(var category in categories)
{
    string[] instanceNames = category.GetInstanceNames();
    foreach(string instanceName in instanceNames)
        PerformanceCounter[] counters = category.GetCounters(instanceName);
}

答案 1 :(得分:2)

这里有一些代码可以修改以获得你想要的计数器(有太多的帖子可以发布)。

将输出所有类别,但只会输出所需类别的计数器。

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

答案 2 :(得分:1)

您可以通过搜索类别+&#34; object&#34;在Microsoft页面上找到它们。 例如,对于内存进程对象,它将是这样的:null: TypeError: toData is not a function message: "toData is not a function" stack: "TypeError: toData is not a function at EventEmitter.<anonymous> (c:\Users\rutherfordc\Documents\GitHub\Migratron\sql-actions.js:36:3) at EventEmitter.emit (events.js:160:13) at pool.request.query.then.res (c:\Users\rutherfordc\Documents\GitHub\Migratron\sql-actions.js:87:23) at <anonymous> at process._tickCallback (internal/process/next_tick.js:160:7)" https://msdn.microsoft.com/en-us/library/ms804621.aspx