【发布时间】:2011-06-09 23:51:08
【问题描述】:
我正在尝试使用 PerformanceCounter 类通过 C# 访问位于 ".NET CLR Memory category" 中的性能计数器。但是,无法使用我期望的正确类别/计数器名称来实例化类别
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。
【问题讨论】:
-
这仅仅是以管理员权限运行的情况,就像奥列格的回答一样?这为我解决了问题。
标签: c# .net memory performancecounter