2

我有一个更新两个性能计数器值的 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 上发现了这个问题,看起来和我的很相似:RateOfCountsPerSecond32 类型的计数器总是显示 0,但它不能解决我的问题。

任何的想法 ?

4

2 回答 2

1

重新启动计算机后,我的代码按预期工作......奇怪!!!!!!!

于 2012-08-09T07:24:13.520 回答
0

我在 C# 中遇到了同样的问题。我还发现重新启动修复了它。

另一件事似乎对我有用,在这样的块中初始化计数器:

counter.BeginInit();
counter.RawValue = 0;
counter.EndInit();

那是 C# 代码,但我猜 powershell 有一组相应的函数。

于 2017-06-20T22:54:20.240 回答