1

I want to check render time of each page in asp.net mvc application.

i am using asp.net tracing. i have override the OnActionExecuting and OnActionExecuted methods on the BaseController class.

protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string controler = filterContext.RouteData.Values["controller"].ToString();
            string action = filterContext.RouteData.Values["action"].ToString();
            StartTime =System.DateTime.Now;

            System.Diagnostics.Trace.Write(string.Format("Start '{0}/{1}' on: {2}", controler, action, System.DateTime.Now.UtilToISOFormat()));
        }
        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            string controler = filterContext.RouteData.Values["controller"].ToString();
            string action = filterContext.RouteData.Values["action"].ToString();
            var totalTime = System.DateTime.Now - this.StartTime;
            System.Diagnostics.Trace.Write(totalTime.ToString());
            System.Diagnostics.Trace.Write(string.Format("End '{0}/{1}' on: {2}", controler, action, System.DateTime.Now.UtilToISOFormat()));

        }

in OnActionExecuted method i get total time. how can i show this time in my http://localhost:51335/Trace.axd report?

Even i had set Trace="true" in <%@ Page %> on every page also.

Pankaj
  • 4,419
  • 16
  • 50
  • 72

1 Answers1

1

Classic ASP.NET uses the following within your aspx code behind: Page.Trace.Write(totalTime.ToString());

In MVC you generally use System.Diagnostics.Trace.Write(... as you have done, so this code is correct.

Try this in your web.config. It routes System.Diagnostics.Trace to ASP.NET tracing:

<system.diagnostics>
<trace>
    <listeners>
       <add name="WebPageTraceListener" 
            type="System.Web.WebPageTraceListener, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
    </listeners>
  </trace>
</system.diagnostics>

Another thing that you could try is using the Controller.HttpContext.Trace instead. I am unable to test that right now as I am writing this from my phone, so please let me know whether that works.

This page shows you more info on how to make all System.Diagnostics.Trace messages appear un your Trace.axd - http://msdn.microsoft.com/en-us/library/b0ectfxd%28v=VS.85%29.aspx

Daniel Dyson
  • 13,192
  • 6
  • 42
  • 73