2012-07-17 61 views
7

我有這段代碼: 我在哪裏創建我的Performance Counter。它執行正常,如果不存在,它也會創建性能計數器,但是當我使用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

過去我遇到的一個與perf計數器有關的問題是,運行的進程必須是管理員,或者具有創建perf計數器的特定權限。這就是爲什麼通常新的性能計數器是在安裝時而不是運行時創建的。我不記得如果你的應用沒有管理權限會發生什麼;它可能只是默默無法創建計數器。雖然我認爲它會拋出一個例外......不過無論如何,如果你還沒有,就試試以管理員身份運行你的應用。 – CodingWithSpike 2012-07-17 18:51:12

+3

此外,如果您在性能監視器運行時創建的計數器,你需要重新啓動性能監視器,使其認識到新的計數器。 – 2012-07-17 18:58:38

+0

另外,計數器不能立即顯示。有時需要幾秒鐘才能看到它們。 – 2012-07-17 18:59:38

回答

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 
     } 

然後將捕獲exceptions.If你看到這樣的異常「請求的註冊表訪問是不允許的。」那麼你需要管理權限來做這些事情。確認此操作使用管理權限運行Visual Studio並執行代碼。

1

除了運行Visual Studio以管理員允許類的創作,我有同樣的問題 - .NET代碼報道,計數器在那裏,但也有在可見的perfmon沒有這樣的計數器類別。

顯然perfmon有時會disable performance counters by flagging it as disabled in the registry

如果你check in the registry根據HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services你應該能夠找到你的性能計數器類別(只需查找您的類別名稱作爲「文件夾」之一)。在子項下(「文件夾」)Performance找到註冊表值Disable Performance Counters並將其設置爲零。重新啓動perfmon,你現在應該在perfmon中看到你的類別和計數器。

相關問題