列出类别中的所有性能计数器 [英] List all performance counters for a category

查看:132
本文介绍了列出类别中的所有性能计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有System.Diagnostics程序用于检索所有实例性能计数器的一个给定的类别名称

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.

我想使仪表板以这样一种方式,它并不需要被更新每当有人增加了一个新的计数器到一块新$ C $的c

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.

推荐答案

试试这个:

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.

这篇关于列出类别中的所有性能计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆