我在我的 WCF 服务中使用 messageInspectors 来测量每个服务方法的经过时间,如下所示:
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
if (_activated)
{
_startTime.Stop();
PerformanceCounterHandler.Instance.SetAveragePerformanceCounter(operationName, _startTime.ElapsedTicks);
}
}
public object BeforeCall(string operationName, object[] inputs)
{
Guid correlationState;
if (_activated)
{
correlationState = Guid.NewGuid();
_startTime = new Stopwatch();
_startTime.Start();
return correlationState;
}
return null;
}
这就是计数器的注册方式
foreach (string methodName in ServiceMethodNames)
{
counter = new CounterCreationData(methodName, methodName + " > Genomsnittlig tid(ms)", PerformanceCounterType.AverageTimer32);
col.Add(counter);
counter = new CounterCreationData(methodName + "_base", methodName + " > Genomsnittlig tid(ms)", PerformanceCounterType.AverageBase);
col.Add(counter);
}
设置性能计数器的方法如下所示:
public Boolean SetAveragePerformanceCounter(string performanceCounterName, long value)
{
PerformanceCounter performCounter;
if (_useOrbitPerformanceCounters && (performCounter = _performanceCounters.Values.Where(c=> c.CounterName == performanceCounterName).FirstOrDefault()) != null)
{
performCounter.IncrementBy(value);
performanceCounterName = performanceCounterName + "_base";
performCounter = _performanceCounters[performanceCounterName];
performCounter.Increment();
return true;
}
return false;
}
然而,性能计数器确实显示峰值而不是平均值?如果我更改视图以报告一切都是 0,只要我什么都不做?即使目前没有呼叫,我也需要能够看到呼叫时间平均值现在的样子。我究竟做错了什么?