问题
I have a WCF service that updates the value of two performance counters. The first is defined as NumberOfItems64, and the second as RateOfCountsPerSecond64. When I update their values (I do this several times per second), perfmon displays as expected the right value for the first counter, but always says that the value of the second counter is 0. When I debug my code, I can see that the RawValue property of the second counter is updated as expected...
Here is my PowerShell code to create the counters :
$categoryName = "My category"
$exists = [System.Diagnostics.PerformanceCounterCategory]::Exists($categoryName)
if ($exists)
{
[System.Diagnostics.PerformanceCounterCategory]::Delete($categoryName)
}
$counters = new-object System.Diagnostics.CounterCreationDataCollection
$counter = new-object System.Diagnostics.CounterCreationData
$counter.CounterType = [System.Diagnostics.PerformanceCounterType]::NumberOfItems64
$counter.CounterName = "# ops"
$counters.Add($counter)
$counter = new-object System.Diagnostics.CounterCreationData
$counter.CounterType = [System.Diagnostics.PerformanceCounterType]::RateOfCountsPerSecond64
$counter.CounterName = "# ops/sec"
$counters.Add($counter)
[System.Diagnostics.PerformanceCounterCategory]::Create($categoryName, $categoryName, [System.Diagnostics.PerformanceCounterCategoryType]::MultiInstance, $counters)
Here is my code to update the value of the counters :
long value = GetValue();
counter1.IncrementBy(value);
counter2.IncrementBy(value);
I found on StackOverflow this question, that looks pretty similar to mine : Counter of type RateOfCountsPerSecond32 always shows 0 but it doesn't resolve my problem.
Any idea ?
回答1:
After a restart of my computer, my code works as expected... Strange !!!!!!!
回答2:
I had the same problem in C#. I also found that a reboot fixed it.
Another thing that seemed to work for me, initializing the counter in a block like this:
counter.BeginInit();
counter.RawValue = 0;
counter.EndInit();
That's C# code, but I'm guessing powershell has a corresponding set of functions.
来源:https://stackoverflow.com/questions/11865894/performance-counter-of-type-rateofcountspersecond64-has-always-the-value-0