ASP.NET MVC和WebForms中的页面构造时间

时间:2021-12-10 03:18:06

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)

我想在页面底部显示一个简单的“内置x.yz秒”文本,以便轻松检查各种选项。 (某些页面是使用“常规”用户在前一页中输入的参数构建的,他们也希望看到他的决定结果)

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.

我知道我可以在asp.net中使用跟踪,但它不是非常用户友好,我担心它对性能不是很好。

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 ?

你认为这个http://www.aspnetzone.de/blogs/peterbucher/archive/2008/03/16/requestdauer-mit-einem-httpmodule-und-response-filter-ausgeben.aspx(httpmodule,德语网页)是个很好的解决方案?

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

哦,既然我现在正在开展几个项目,我想为asp.net mvc页面和webforms :)做这件事。

Thanks !

2 个解决方案

#1


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

HttpContext对象包含“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

如果您想要最精确的时间,可以在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.

将它记录到log4net,或者其他一些。

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.

当然,如果你需要在页面底部显示它,而不是简单地让信息来监控健康状况,那么你可以在页面上手动减去它。

#2


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)

可以阅读德语,但我可能只需将当前的DateTime存储在页面的构造函数中,然后在OnPreRender事件中打印当前时间和存储时间之间的时间差,即可显示时间的标签。 - OnPreRender事件是页面生命周期中的最后一点,它可以改变页面的呈现内容(不进行某种后处理)

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

只要您在标签的OnPreRender事件后没有进行任何繁重的处理,这应该可以正常工作。

#1


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

HttpContext对象包含“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

如果您想要最精确的时间,可以在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.

将它记录到log4net,或者其他一些。

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.

当然,如果你需要在页面底部显示它,而不是简单地让信息来监控健康状况,那么你可以在页面上手动减去它。

#2


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)

可以阅读德语,但我可能只需将当前的DateTime存储在页面的构造函数中,然后在OnPreRender事件中打印当前时间和存储时间之间的时间差,即可显示时间的标签。 - OnPreRender事件是页面生命周期中的最后一点,它可以改变页面的呈现内容(不进行某种后处理)

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

只要您在标签的OnPreRender事件后没有进行任何繁重的处理,这应该可以正常工作。