ASP Web API操作的日志持续时间 [英] Log duration of an ASP Web API action
本文介绍了ASP Web API操作的日志持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用ActionFilter记录我的ASP.NET Web API项目的所有操作调用. OnActionExecuted方法可以告诉很多正在发生的事情.
I am using an ActionFilter to log all action calls of my ASP.NET Web API project. The OnActionExecuted method tells a lot about what's been happening.
我只是不知道如何找到一种有效的方法来测量执行时间...
I just can't figure out how to find an efficient way to measure execution time...
感谢您的帮助!
推荐答案
这种方法应该可以解决问题...
Something like this should do the trick...
public class StopwatchAttribute : ActionFilterAttribute
{
private const string StopwatchKey = "StopwatchFilter.Value";
public override void OnActionExecuting(HttpActionContext actionContext)
{
base.OnActionExecuting(actionContext);
actionContext.Request.Properties[StopwatchKey] = Stopwatch.StartNew();
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
base.OnActionExecuted(actionExecutedContext);
Stopwatch stopwatch = (Stopwatch)actionExecutedContext.Request.Properties[StopwatchKey];
// TODO something useful with stopwatch.Elapsed
Trace.WriteLine("Elapsed = " + stopwatch.Elapsed);
}
}
在这里,我们将新的Stopwatch
存储在请求属性中,并在请求完成后将其停止.
Here we store a new Stopwatch
in the request properties and stop it when the request has completed.
这篇关于ASP Web API操作的日志持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文