如何在asp.net中显示页面生成时间?

时间:2022-07-07 15:18:05

What is the easiest way to monitor how long it is taking to render my asp.net pages (I'm using webforms still if it matters)?

什么是监视渲染我的asp.net页面需要多长时间的最简单方法(如果重要的话,我还在使用webforms)?

I know that the page lifecycle is pretty involved, but I'm basically just looking for one number that tells me how long my code took to run and to display that at the bottom (or wherever) of the page.

我知道页面生命周期很复杂,但我基本上只是在寻找一个数字,告诉我我的代码运行了多长时间并在页面的底部(或任何地方)显示。

Edit

Several people have mentioned the asp.net trace config, which is awesome and I'm not sure how I missed it. The one thing I am still looking for is how to output just the final render time: ie

有几个人提到了asp.net跟踪配置,这很棒,而且我不确定我是怎么错过它的。我还在寻找的一件事就是如何输出最终的渲染时间:即

Response.Write(Context.Trace.FinalRenderTimeFromFirst)

But I can't figure out how to inspect the contents of the trace element.

但我无法弄清楚如何检查trace元素的内容。

4 个解决方案

#1


You want to enable tracing.

您想要启用跟踪。

From The Basics of .NET Tracing:

从.NET跟踪的基础知识:

[M]odify the trace element in web.config as follows:

[M]在web.config中修改trace元素,如下所示:

<trace enabled="true" .../>

#2


I use Fiddler (http://www.fiddler2.com/Fiddler2/version.asp) for such things. It's not specific for ASP.NET, but it will break down your page as it get rendered to the client, and show the amount of time it took to load each piece of your page as it comes in to the browser.

我使用Fiddler(http://www.fiddler2.com/Fiddler2/version.asp)来做这些事情。它不是特定于ASP.NET,但它会在呈现给客户端时分解页面,并显示在页面进入浏览器时加载每个页面所花费的时间。

Hope this helps!

希望这可以帮助!

#3


put trace=true in the first line of you .aspx file and use response.trace("") in your code behind to add additonal information to it.

将trace = true放在.aspx文件的第一行,并在后面的代码中使用response.trace(“”)为其添加附加信息。

#4


I usually add this simple code to the bottom of my Layout view:

我通常将这个简单的代码添加到Layout视图的底部:

<!-- Page generated in @((DateTime.UtcNow - HttpContext.Current.Timestamp.ToUniversalTime()).TotalSeconds.ToString("F4")) s -->

I am using ASP.NET MVC with Razor, but should be easy trivial to port to Web Forms too.

我正在使用带有Razor的ASP.NET MVC,但是也很容易移植到Web窗体。

Outputs something like:

输出类似于:

<!-- Page generated in 0.4399 s -->

This starts measuring when the HttpContext is created (according to the documentation), and stops right before writing to output, so it should be pretty accurate.

这开始测量何时创建HttpContext(根据文档),并在写入输出之前停止,因此它应该非常准确。

It might not work for all use cases (if you want to measure child actions, or ignore the time outside the MVC action, etc.), but it's easy to paste into a view.

它可能不适用于所有用例(如果您想测量子操作,或忽略MVC操作之外的时间等),但很容易粘贴到视图中。

For more advanced diagnostics, I used to use Glimpse.

对于更高级的诊断,我曾经使用过Glimpse。

#1


You want to enable tracing.

您想要启用跟踪。

From The Basics of .NET Tracing:

从.NET跟踪的基础知识:

[M]odify the trace element in web.config as follows:

[M]在web.config中修改trace元素,如下所示:

<trace enabled="true" .../>

#2


I use Fiddler (http://www.fiddler2.com/Fiddler2/version.asp) for such things. It's not specific for ASP.NET, but it will break down your page as it get rendered to the client, and show the amount of time it took to load each piece of your page as it comes in to the browser.

我使用Fiddler(http://www.fiddler2.com/Fiddler2/version.asp)来做这些事情。它不是特定于ASP.NET,但它会在呈现给客户端时分解页面,并显示在页面进入浏览器时加载每个页面所花费的时间。

Hope this helps!

希望这可以帮助!

#3


put trace=true in the first line of you .aspx file and use response.trace("") in your code behind to add additonal information to it.

将trace = true放在.aspx文件的第一行,并在后面的代码中使用response.trace(“”)为其添加附加信息。

#4


I usually add this simple code to the bottom of my Layout view:

我通常将这个简单的代码添加到Layout视图的底部:

<!-- Page generated in @((DateTime.UtcNow - HttpContext.Current.Timestamp.ToUniversalTime()).TotalSeconds.ToString("F4")) s -->

I am using ASP.NET MVC with Razor, but should be easy trivial to port to Web Forms too.

我正在使用带有Razor的ASP.NET MVC,但是也很容易移植到Web窗体。

Outputs something like:

输出类似于:

<!-- Page generated in 0.4399 s -->

This starts measuring when the HttpContext is created (according to the documentation), and stops right before writing to output, so it should be pretty accurate.

这开始测量何时创建HttpContext(根据文档),并在写入输出之前停止,因此它应该非常准确。

It might not work for all use cases (if you want to measure child actions, or ignore the time outside the MVC action, etc.), but it's easy to paste into a view.

它可能不适用于所有用例(如果您想测量子操作,或忽略MVC操作之外的时间等),但很容易粘贴到视图中。

For more advanced diagnostics, I used to use Glimpse.

对于更高级的诊断,我曾经使用过Glimpse。