views:

102

answers:

2

I want to display at the bottom of my pages a simple "built in x.yz seconds" text, to easily check various options. (some pages are built using parameters entered in a previous page by a "regular" user, who would like too to see the results of his decisions)

I know I can use traces in asp.net, but it's not very user-friendly, and I fear it's not very good for performance.

Do you think this http://www.aspnetzone.de/blogs/peterbucher/archive/2008/03/16/requestdauer-mit-einem-httpmodule-und-response-filter-ausgeben.aspx (httpmodule, webpage in german) is a good solution ?

Oh, and since I'm working on several projects right now, I'd like to do it for asp.net mvc pages, and webforms :)

Thanks !

A: 

Definitely dont use traces in production code!

Can read german, but I would probably implement this by simply storing the current DateTime in the constructor of the page and then printing the time difference between the current time and that stored time in the OnPreRender event for the label thats going to be showing the time - the OnPreRender event is the last point in the pages lifecycle where its possible to alter the rendered contents of the page (without doing some sort of post-processing)

This should work fine as long as you don't do any heavy processing after the OnPreRender event of your label.

Kragen
A: 

The HttpContext object contains a timestamp for "start-of-http-request".

I guess this will usually be good enough, unless its important for you to subtract any time spent waiting for an available thread to become available.

So Build time would be

    DateTime.Now.Subtract(HttpContext.Current.Timestamp).TotalMilliseconds

If you want the most precise time, you can manually store the start time in global.asax

    void Application_PreRequestHandlerExecute(object sender, EventArgs e)
    { 
        HttpContext.Current.Session.Add("LastPageStart", DateTime.Now);
    }

and subtract it from the end time at

   Application_EndRequest

The log it to log4net, or some such.

Ofcourse, if you need to show it on the bottom of the page, instead of simply having the info to monitor health for, then you could probably just subtract it manually on the page.

Soraz