C#:访问PerformanceCounters为" .NET CLR内存种类" [英] C#: Accessing PerformanceCounters for the ".NET CLR Memory category"

查看:159
本文介绍了C#:访问PerformanceCounters为" .NET CLR内存种类"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访​​问位于性能计数器。 NET CLR存储类使用PerformanceCounter类通过C#的。然而,一个不能实例化的类别,有什么我会想到的是正确的类别/计数器名称

I'm trying to access the performance counters located in ".NET CLR Memory category" through C# using the PerformanceCounter class. However a cannot instantiate the categories with what I would expect was the correct category/counter name

new PerformanceCounter(".NET CLR Memory", "# bytes in all heaps", Process.GetCurrentProcess().ProcessName);

我试着用下面的code通过分类和计数循环

I tried looping through categories and counters using the following code

string[] categories = PerformanceCounterCategory.GetCategories().Select(c => c.CategoryName).OrderBy(s => s).ToArray();
string toInspect = string.Join(",\r\n", categories);

System.Text.StringBuilder interestingToInspect = new System.Text.StringBuilder();
string[] interestingCategories = categories.Where(s => s.StartsWith(".NET") || s.Contains("Memory")).ToArray();
foreach (string interestingCategory in interestingCategories)
{
    PerformanceCounterCategory cat = new PerformanceCounterCategory(interestingCategory);
    foreach (PerformanceCounter counter in cat.GetCounters())
    {
        interestingToInspect.AppendLine(interestingCategory + ":" + counter.CounterName);
    }
}
toInspect = interestingToInspect.ToString();

但找不到任何东西,这似乎匹配。这难道不是可以从CLR中观察到这些价值还是我做错了什么。

But could not find anything that seems to match. Is it not possible to observe these values from within the CLR or am I doing something wrong.

的环境中,应该有关系,是.NET 4.0在64位Windows上运行7中。

The environment, should it matter, is .NET 4.0 running on a 64-bit windows 7 box.

推荐答案

您需要以管理员身份访问.NET CLR存储类运行程序。

You need to run the program as Administrator to access .NET CLR Memory category.

试试这个简单的测试,使用PowerShell的:

Try this simple test, using powershell:

当以管理员身份运行

[Diagnostics.PerformanceCounterCategory] ​​::存在(.NET CLR内存)

[Diagnostics.PerformanceCounterCategory]::Exists(".NET CLR Memory")

在没有管理权限运行:

[Diagnostics.PerformanceCounterCategory] ​​::存在(.NET CLR内存)

[Diagnostics.PerformanceCounterCategory]::Exists(".NET CLR Memory")

这篇关于C#:访问PerformanceCounters为" .NET CLR内存种类"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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