メソッドの完了時間を記録するために Ninject.Extensions.Interception.DynamixProxy を使用してインターセプターを作成しようとしています。
シングルスレッド環境では、次のように機能します。
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 が呼び出し間で共有されるため、これは機能しません。呼び出し間で共有されないように、StopWatch のインスタンスを BeforeInvoke から AfterInvoke に渡す方法は?