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

最佳答案

没有收到任何异常的原因是缺少try-catch块。如果您在try and 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并执行代码。

关于c# - 我的绩效计数器在哪里?它已创建,但我在perfmon中看不到它,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11528804/

10-15 22:16