我有这段代码:我在哪里创建我的性能计数器。它执行正常,如果不存在,它也会创建性能计数器,但当我使用perfmon时,我找不到这个性能计数器。
这是怎么回事?
const string _categoryName = "MyPerformanceCounter";
if (!PerformanceCounterCategory.Exists(_categoryName))
{
CounterCreationDataCollection counters = new CounterCreationDataCollection();
CounterCreationData ccdWorkingThreads = new CounterCreationData();
ccdWorkingThreads.CounterName = "# working threads";
ccdWorkingThreads.CounterHelp = "Total number of operations executed";
ccdWorkingThreads.CounterType = PerformanceCounterType.NumberOfItems32;
counters.Add(ccdWorkingThreads);
// create new category with the counters above
PerformanceCounterCategory.Create(_categoryName,
"Performance counters of my app",
PerformanceCounterCategoryType.SingleInstance,
counters);
}发布于 2012-10-01 19:11:47
没有收到任何异常的原因是缺少try-catch块。如果将语句添加到try和catch块中,如下所示
try
{
const string _categoryName = "MyPerformanceCounter";
if (!PerformanceCounterCategory.Exists(_categoryName))
{
CounterCreationDataCollection counters =
new CounterCreationDataCollection();
CounterCreationData ccdWorkingThreads = new CounterCreationData();
ccdWorkingThreads.CounterName = "# working threads";
ccdWorkingThreads.CounterHelp = "Total number of operations executed";
ccdWorkingThreads.CounterType = PerformanceCounterType.NumberOfItems32;
counters.Add(ccdWorkingThreads);
// create new category with the counters above
PerformanceCounterCategory.Create(_categoryName,
"Performance counters of my app",
PerformanceCounterCategoryType.SingleInstance,
counters);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString()); //Do necessary action
} 然后,它将捕获您看到的exceptions.If异常,如“请求的注册表访问不允许”。然后,您需要管理员权限才能执行此操作。要确认这一点,请使用管理权限运行Visual Studio并执行代码。
发布于 2014-10-09 22:26:53
除了以管理员身份运行Visual Studio以允许创建类别之外,我还遇到了同样的问题- .NET代码报告计数器在那里,但在perfmon中看不到这样的计数器类别。
显然,perfmon有时会disable performance counters by flagging it as disabled in the registry。
如果您在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services下使用check in the registry,您应该能够找到您的性能计数器类别(只需在“文件夹”中查找您的类别名称)。在子项("folder") Performance下,找到注册表值Disable Performance Counters并将其设置为零。重新启动perfmon,您现在应该可以在perfmon中看到您的类别和计数器。
https://stackoverflow.com/questions/11528804
复制相似问题