【问题标题】:Where are my Performance Counter? It is created but I can't see it in perfmon我的性能计数器在哪里?它已创建,但我在 perfmon 中看不到它
【发布时间】:2012-07-17 18:41:19
【问题描述】:

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

【问题讨论】:

  • 过去我在使用性能计数器时遇到的一个问题是,正在运行的进程必须是管理员,或者具有创建性能计数器的特定权限。这就是为什么通常在安装时而不是运行时创建新的性能计数器的原因。我不记得如果您的应用程序没有管理员权限会发生什么;它可能只是默默地无法创建计数器。虽然我认为它会引发异常......但无论如何,如果您还没有,请尝试以管理员身份运行您的应用程序。
  • 另外,如果您在 perfmon 运行时创建计数器,则需要重新启动 perfmon 以使其识别新的计数器。
  • 另外,计数器不会立即可见。有时需要几秒钟才能看到它们。
  • 所以,PerformanceCounterCategory.Create() 可以什么都不做,不抛出异常。
  • 我刚刚在 Windows 7 上运行了您的代码,并且该类别已按预期创建。而且您并没有真正创建PerformanceCounter,您只是在定义一个PerformanceCounterCategory。尝试创建一个 PerformanceCounter 来确定是否引发了异常。

标签: c# .net perfmon


【解决方案1】:

没有收到任何异常的原因是缺少 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 并执行代码。

【讨论】:

    【解决方案2】:

    除了以管理员身份运行 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 中看到您的类别和计数器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      相关资源
      最近更新 更多