2011-06-02 7 views
2

J'ai un peu de mal à incrémenter un compteur de performance personnalisé sur Windows 7. La catégorie est créée, les compteurs sont créés, mais en appelant "Increment()" ou en définissant "RawValue" n'a aucun effet apparent. Par exemple, la valeur de retour de Increment() reste 0L.Incrémentation des compteurs de performance personnalisés .net 4.0 Windows 7

Le code suivant est une application console qui devra être exécutée en tant qu'administrateur pour créer la catégorie. Le débogage à travers démontre le symptôme ci-dessus.

Toute aide est grandement appréciée.

class Program 
{ 
    private static string _category = "MyCustomCounters"; 

    static void Main(string[] args) 
    { 
     var deleteIfExists = false; 

     if (args.Any()) 
     { 
      if (args.Count() > 0) 
      { 
       _category = args[0]; 
      } 

      if (args.Count() > 1) 
      { 
       deleteIfExists = args[1].ToUpper() == bool.TrueString.ToUpper(); 
      } 
     } 

     CreateCustomCounters(_category, deleteIfExists); 
    } 

    private static void CreateCustomCounters(string category, bool deleteIfExists) 
    { 
     if (deleteIfExists && PerformanceCounterCategory.Exists(category)) 
     { 
      PerformanceCounterCategory.Delete(category); 
     } 

     if (!PerformanceCounterCategory.Exists(category)) 
     { 
      CounterCreationDataCollection counterCollection = new CounterCreationDataCollection(); 

      // add a counter tracking operations per second 
      CounterCreationData opsPerSec = new CounterCreationData(); 
      opsPerSec.CounterName = "# requests /sec"; 
      opsPerSec.CounterHelp = "Number of requests executed per second"; 
      opsPerSec.CounterType = PerformanceCounterType.RateOfCountsPerSecond32; 
      counterCollection.Add(opsPerSec); 

      // add a counter tracking operations per second 
      CounterCreationData operationTotal = new CounterCreationData(); 
      operationTotal.CounterName = "Total # requests"; 
      operationTotal.CounterHelp = "Total number of requests executed"; 
      operationTotal.CounterType = PerformanceCounterType.NumberOfItems32; 
      counterCollection.Add(operationTotal); 

      PerformanceCounterCategory.Create(category, 
         "A custom counter category that tracks execution", PerformanceCounterCategoryType.SingleInstance, 
         counterCollection); 
     } 
     else 
     { 
      Console.Error.WriteLine("Counter already exists, try specifying parameters for categoryname and or insisting on deleting"); 
     } 

     using (var _totalOperationsCounter = new PerformanceCounter(_category, "Total # requests", false)) 
     { 
      _totalOperationsCounter.ReadOnly = false; 
      _totalOperationsCounter.RawValue = 10; 
      var count = _totalOperationsCounter.Increment(); 
     } 
    } 
} 

Répondre

1

pour moi le comportement est comme follwed (je ne mets pas le rawvalue)

  • lorsque le moniteur de performance ne fonctionne pas
    • Console.WriteLine(_totalOperationsCounter.Increment()) affiche toujours 1
  • quand le moniteur de performance fonctionne
    • Console.WriteLine(_totalOperationsCounter.Increment()) incréments entre les applications s'exécute
Questions connexes