如何在Asp.net MVC中获取请求处理时间? [英] How to get the request processing time in Asp.net mvc?
问题描述
我想记录 Asp.Net MVC应用程序的请求处理时间.我可以从IIS日志中获取"<花费时间>",但是该时间包括将响应发送回客户端浏览器所花费的网络时间.我只对asp.net worker进程处理请求所花的时间感兴趣.
I want to log the request processing time for Asp.Net MVC application. I can get 'time-taken' from IIS logs but this time includes the network time taken to send the response back to client browser. I am only interested in the time taken to preocess the request by asp.net worker process.
我可以选择编写 HttpModule ,但是我无法控制HttpModules的运行顺序.
I have an option of writting a HttpModule but I can not control the order of running of HttpModules.
更新-1 :
我需要记录所有请求的处理时间.请求仅对某些用户缓慢,当我请求页面时,这些用户无法复制. "MiniProfiler"和"Glimpse"不会为不同用户的所有请求提供处理时间".
I need to log the processing-time for all the requests. The requests are slow only for some users which I am not able to reproduce when I request the page. 'MiniProfiler' and 'Glimpse' will not give me the 'procssing-time' for all the requests by different users.
更新-2 :
过滤器只会给我耗时"的控制器.它不会涵盖Asp.Net应用程序中插入的各种"HttpModules"所花费的时间"
Filters will give me 'time-taken' only by Controller. It will not cover the 'time-taken' by various 'HttpModules' pluged in into the Asp.Net app
推荐答案
您可以创建一个过滤器来拦截动作执行时间.
You can create a filter to intercept action execution time.
public class ActionTiming :ActionFilterAttribute {
private static readonly ILog Logger = LogManager.GetLogger("TimingLogger");
private string controllerName = string.Empty;
private string actionName = string.Empty;
private Stopwatch intertime = Stopwatch.StartNew();
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
intertime.Restart();
controllerName = filterContext.RouteData.Values["controller"].ToString();
actionName = filterContext.RouteData.Values["action"].ToString();
var message = string.Format("{0} controller: {1} action: {2}", "OnActionExecuting", controllerName, actionName);
Logger.Info(message);
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
var message = string.Format("{0} controller: {1} action: {2}, time (ms): {3}", "OnResultExecuted", controllerName, actionName, intertime.ElapsedMilliseconds);
Logger.Info(message);
}
}
然后,您可以在Application_Start中全局注册此过滤器,或将其用作属性.
Then, you can register this filter globally in Application_Start or use it as attribute.
这篇关于如何在Asp.net MVC中获取请求处理时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!