我正在使用 ActionFilter 来记录 ASP.NET Web API 项目的所有操作调用。 OnActionExecuted 方法讲述了很多有关正在发生的事情的信息。
我只是不知道如何找到一种有效的方法来测量执行时间......
像这样的事情应该可以解决问题......
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
在请求属性中,并在请求完成时停止它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)