Rn2dy
Rn2dy

Reputation: 4190

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 original 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));
    }

Upvotes: 0

Views: 2441

Answers (2)

Kenny Eliasson
Kenny Eliasson

Reputation: 2052

Make an actionfilter instead. I've pasted some code over here.

ActionFilters are a lot easier to reuse and can be implemented globally.

Upvotes: 1

Baldy
Baldy

Reputation: 3659

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

Upvotes: 0

Related Questions