Reputation: 654
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.
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.
Any help is greatly appreciated.
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();
}
}
}
Upvotes: 3
Views: 2098
Reputation: 10940
for me the behavior is as follwed (I do not set the RawValue)
Console.WriteLine(_totalOperationsCounter.Increment())
displays always 1Console.WriteLine(_totalOperationsCounter.Increment())
increments between applications runsUpvotes: 1