I want to calculate the execution time of my ASP.NET pages and display it on the page. Currently I'm calculating the execution time using a System.Diagnostics.Stopwatch and then store the value in a log database.
The stopwatch is started in OnInit and stopped in OnPreRenderComplete. This seems to be working quite fine, and it's giving a similar execution time as the one shown in the page trace.
The problem now is that I'm not able to display the execution time on the page because the stopwatch is stopped too late in the life cycle.
What is the best way to do this?
I would get the elapsed time from the stopwatch during render time. Most (likely almost all) of the page execution time has elapsed at this point. Go ahead and stop the stop watch where you are now and use that number to log to your DB. I'd bet it won't be much different from what gets displayed on the page.