title: Incrementing Custom Performance Counters .net 4.0 Windows 7
body: <p>I'm having a little trouble incrementing a custom performance counter on Windows 7. The Category is created, the Counters are created, but calling "Increment()" or setting "RawValue" has no seeming effect. For instance, the return value of Increment() remains 0L.</p>
  
  <p>The following code is a console App, that will need to be run as an administrator to create the category. Debugging through demonstrates the above symptom.</p>
  
  <p>Any help is greatly appreciated.</p>
  
  <pre><code>class Program
  {
      private static string _category = "MyCustomCounters";
  
      static void Main(string[] args)
      {
          var deleteIfExists = false;
  
          if (args.Any())
          {
              if (args.Count() &gt; 0)
              {
                  _category = args[0];
              }
  
              if (args.Count() &gt; 1)
              {
                  deleteIfExists = args[1].ToUpper() == bool.TrueString.ToUpper();
              }
          }
  
          CreateCustomCounters(_category, deleteIfExists);
      }
  
      private static void CreateCustomCounters(string category, bool deleteIfExists)
      {
          if (deleteIfExists &amp;&amp; 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();
          }
      }
  }
  </code></pre>