我的表演台在哪里?它已创建,但我无法在perfmon中看到它

时间:2012-07-17 18:41:19

标签: c# .net perfmon

我有这段代码: 我在哪里创建我的性能计数器。它执行正常,如果不存在,它也会创建性能计数器,但是当我使用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);
    }

2 个答案:

答案 0 :(得分:2)

没有收到任何异常的原因是缺少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
        }   

然后它将捕获异常。如果您看到“不允许请求的注册表访问”之类的异常。那么你需要管理权来做这些事情。确认此操作运行具有管理权限的Visual Studio并执行代码。

答案 1 :(得分:1)

除了以管理员身份运行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,您应该能够找到您的效果计数器类别(只需将您的类别名称作为其中一个"文件夹")。在子项下("文件夹")Performance找到注册表值Disable Performance Counters并将其设置为零。重新启动perfmon,您现在应该在perfmon中看到您的类别和计数器。