我創建了一個PostSharp方面,它應該記錄我使用它的任何方法的執行時間。在PostSharp中使用StopWatch進行異步方法的不準確性能日誌記錄MethodInterceptionAspect
但是,它似乎沒有按照我的預期工作,sw.ElapsedMilliseconds
總是在0到3毫秒之間。
[Serializable]
[AttributeUsage(AttributeTargets.Method)]
public sealed class PerfLogAttribute : MethodInterceptionAspect
{
public override void OnInvoke(MethodInterceptionArgs args)
{
var sw = new Stopwatch();
sw.Start();
args.Proceed();
sw.Stop();
log.Debug(sw.ElapsedMilliseconds);
}
}
使用這樣的:
[PerfLog]
public async Task<bool> DoSomethingAsync() {
// Adding a delay to test (or call database async)
await Task.Delay(5000);
return true;
}
你確定Postsharp以async/await的方式工作嗎?它可能只是測量到「等待Task.Delay(5000)」,不包括它。使用Google搜索「PostSharp異步MethodInterceptor」可以找到一些可能相關的點擊。 –