In ASP.NET MVC 3, How can I use OnResultExecuting/OnresultExecuted method to trace the page load time?
I put OnResultExecuting/OnResultExecuted code in my controller and I enabled trace in web.config, but nothing was shown on any of the pages for the controller about the trace information (I just got origina开发者_运维问答l page, appending trace.axd does not work either!)
The web.config to enable trace:
<system.web><trace enabled="true" pageOutput="true" requestLimit="40" localOnly="false"/></system.web>
OnResultExecuting/OnResultExecuted:
protected override void OnResultExecuting(ResultExecutingContext filterContext)
{
string controller = filterContext.RouteData.Values["controller"].ToString();
string action = filterContext.RouteData.Values["action"].ToString();
start_time = DateTime.Now;
System.Diagnostics.Trace.Write(string.Format("Start '{0}/{1}' on: {2}", controller, action, start_time));
}
protected override void OnResultExecuted(ResultExecutedContext filterContext)
{
string controller = filterContext.RouteData.Values["controller"].ToString();
string action = filterContext.RouteData.Values["action"].ToString();
var elapsed_time = DateTime.Now - start_time;
System.Diagnostics.Trace.Write(string.Format("Start '{0}/{1}' on: {2}", controller, action, elapsed_time));
}
Make an actionfilter instead. I've pasted some code over here.
ActionFilters are a lot easier to reuse and can be implemented globally.
Do you know about the mvc mini profiler - http://code.google.com/p/mvc-mini-profiler/
It was written by sam saffron who develops this very site.
Sounds like it may already do what you are trying to achieve
精彩评论