我有一个WCF服务,用于更新两个性能计数器的值。第一个定义为NumberOfItems64,第二个定义为RateOfCountsPerSecond64。当我更新它们的值(我每秒这样做几次)时,perfmon按预期显示第一个计数器的正确值,但总是说第二个计数器的值是0.当我调试我的代码时,我可以看到第二个计数器的RawValue属性按预期更新...
以下是我创建计数器的PowerShell代码:
$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)
这是我更新计数器值的代码:
long value = GetValue();
counter1.IncrementBy(value);
counter2.IncrementBy(value);
我在StackOverflow上找到了这个问题,看起来与我的相似:Counter of type RateOfCountsPerSecond32 always shows 0但它无法解决我的问题。
有什么想法吗?
答案 0 :(得分:1)
重新启动计算机后,我的代码按预期工作......奇怪!!!!!!!
答案 1 :(得分:0)
我在C#中遇到了同样的问题。我还发现重启修复了它。
另一件似乎对我有用的事情是,在这样的块中初始化计数器:
counter.BeginInit();
counter.RawValue = 0;
counter.EndInit();
这是C#代码,但我猜测powershell有一套相应的功能。