如何查看/调试System.Diagnostics计数器?

时间:2013-01-21 14:22:44

标签: c#

有没有办法查看应用程序注册的所有性能计数器?

1 个答案:

答案 0 :(得分:0)

是的,您可以查看PerformanceCounterCategory课程中的GetCounter()方法。您将获取PerformanceCounter[]以及每个计数器的一些信息。

PerformanceCounterCategory pcc = new PerformanceCounterCategory();

// Retrieves the list of performance object instances that are associated with this category.
foreach (string instanceName in pcc.GetInstanceNames()) 
    // Retrieves a list of the counters in a performance counter category that contains exactly one instance.
    foreach (PerformanceCounter counter in pcc.GetCounters())
    {
        // now you have the counter object that represents a PerformanceCounter to get some information about the performance counter

        Console.WriteLine("Category: " + counter.Category);
        Console.WriteLine("Instance Name: " + counter.InstanceName);    
        Console.WriteLine("Machine Name: " + counter.MachineName);

        Console.WriteLine("Counter Name: " + counter.CounterName);

        Console.WriteLine("Next Value: " + counter.NextValue());
    }