我想用Ninject.Extensions.Interception.DynamixProxy创建一个拦截器来记录方法完成时间。多线程环境中的Ninject拦截
在单线程环境是这样工作的:
public class TimingInterceptor : SimpleInterceptor
{
readonly Stopwatch _stopwatch = new Stopwatch();
private bool _isStarted;
protected override void BeforeInvoke(IInvocation invocation)
{
_stopwatch.Restart();
if (_isStarted) throw new Exception("resetting stopwatch for another invocation => false results");
_isStarted = true;
invocation.Proceed();
}
protected override void AfterInvoke(IInvocation invocation)
{
Debug.WriteLine(_stopwatch.Elapsed);
_isStarted = false;
}
}
在多线程的情况下,这将然而,无法工作,因为秒表的调用共享。如何将StopWatch的实例从BeforeInvoke传递到AfterInvoke,以便在调用之间不共享?