我需要在 IHandleMessages<> 接口的每个实例中测量 Handle 方法的调用时间。我尝试使用温莎城堡的拦截器,
public class NsbHandlerMeasurementInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
if (invocation.Method.Name == ExpressionExtender.GetMethodName<IHandleMessages<DummyType>>(b => b.Handle(null)))
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
invocation.Proceed();
stopwatch.Stop();
// save stopwatch.ElapsedMilliseconds value
}
else
{
invocation.Proceed();
}
}
}
带安装代码:
container.Register(Component.For<NsbHandlerMeasurementInterceptor>());
container.Kernel.ComponentModelBuilder.AddContributor(new NsbWindsorModelConstructionContributor());
public class NsbWindsorModelConstructionContributor : IContributeComponentModelConstruction
{
public void ProcessModel(global::Castle.MicroKernel.IKernel kernel, global::Castle.Core.ComponentModel model)
{
if (model.Services.Any(s => s.ImplementsGenericInterface(typeof(IHandleMessages<>))))
{
model.Interceptors.AddIfNotInCollection(new InterceptorReference(typeof(NsbHandlerMeasurementInterceptor)));
}
}
}
但从那一刻起,Handle 方法就没有触发。
我知道 NSB 中的性能计数器,但我需要更具体的、面向类型的测量。有可能并且可以实现吗?