2012-07-17 20 views
7

私はこのコードを持っています: 私はパフォーマンスカウンタを作成します。 OKを実行します。存在しない場合はパフォーマンスカウンタも作成しますが、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

1つの問題は、実行中のプロセスが管理者である、またはPERFカウンターを作成するための特定の権限を有していなければならないことです。このため、通常、実行時ではなくインストール時に新しいperfカウンタが作成されます。あなたのアプリに管理者権限がないとどうなるのか覚えていない。それは単に静かにカウンタを作成することができないかもしれません。私はそれが例外を投げると思うだろう...しかし、とにかく、あなたがまだない場合は、あなたのアプリを管理者として実行してみてください。 – CodingWithSpike

+3

また、perfmonの実行中にカウンタを作成する場合、perfmonを再起動して新しいカウンタを認識させる必要があります。 –

+0

さらに、カウンターはすぐに表示されません。時々、それらを見ることができるのに数秒かかることがあります。 –

答えて

2

例外を受け取らない理由は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 
     } 

のようにtryおよびcatchブロックであなたの文を追加する場合、それはあなたのような例外を参照exceptions.Ifをキャプチャします「要求されたレジストリアクセスは許可されていませんが。」そのようなことをするには管理者権限が必要です。これを確認するには管理者権限でVisual Studioを実行し、コードを実行します。

1

は別にカテゴリの作成を許可するには、管理者としてのVisual Studioを実行しているから、私は同じ問題を持っていた - .NETコードはカウンターがあったことを報告したが、パフォーマンスモニタで見えるそのようなカウンターカテゴリがありませんでした。

どうやらperfmonの意志時々disable performance counters by flagging it as disabled in the registry

check in the registryHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Servicesの場合、パフォーマンスカウンターのカテゴリを見つけることができます(カテゴリー名を「フォルダー」の1つとして探してください)。サブキー( "folder")の下でPerformanceレジストリ値Disable Performance Countersを見つけてゼロに設定します。 perfmonを再起動すると、perfmonでカテゴリとカウンタが表示されます。私は過去にPERFカウンターでに実行した

関連する問題