gpt4 book ai didi

C#:访问 ".NET CLR Memory category"的 PerformanceCounters

转载 作者:IT王子 更新时间:2023-10-28 23:34:17 25 4
gpt4 key购买 nike

我正在尝试访问位于 ".NET CLR Memory category" 中的性能计数器通过 C# 使用 PerformanceCounter 类。但是,无法使用我期望的正确类别/计数器名称来实例化类别

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

我尝试使用以下代码循环遍历类别和计数器

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 中观察这些值,还是我做错了什么。

如果重要的话,环境是在 64 位 Windows 7 机器上运行的 .NET 4.0。

最佳答案

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

使用 powershell 试试这个简单的测试:

以管理员身份运行时

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

True

在没有管理权限的情况下运行:

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

False

关于C#:访问 ".NET CLR Memory category"的 PerformanceCounters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4705698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com