ASP.NET TRACE是一个不错的功能可以收集下列信息
Request Details
The Request Details section displays general information about the current request and response.
| Value | Description |
|---|---|
Session ID |
The session identification for the specified request. |
Time of Request |
The time the request was made. |
Request Encoding |
The character encoding for the request. |
Request Type |
The HTTP method used (GET or POST). |
Status Code |
The status-code value associated with the response. For more information, see RFC 2616 at the World Wide Web Consortium (W3C) Web site. |
Response Encoding |
The character encoding for the response. |
Trace Information
The Trace Information section displays the flow of page-level events. If you have created custom trace messages, the messages are displayed in the Trace Information section also.
| Value | Description |
|---|---|
Category |
The custom trace category specified in a Write method call, if any. |
Message |
The custom trace message specified in a Warn or Write method, if any. |
From First (s) |
The elapsed time in seconds since the first trace message was processed. The first trace message appears at the top of the list. |
From Last (s) |
The elapsed time in seconds between the processing of the current trace message and the preceding trace message. |
Control Tree
The Control Tree section displays information about ASP.NET server controls created in the page.
| Value | Description |
|---|---|
Control ID |
The identification for the control. If you have not specified an UniqueID property. |
Type |
The fully qualified type of the control. |
Render Size bytes |
The size in bytes of the rendered control (including child controls). This is the size of the actual HTML, XML, or other format that is sent to the browser. |
ViewState Size bytes |
The size in bytes of the control's view state (excluding child controls). For more information, seeASP.NET State Management. |
ControlState Size bytes |
The size in bytes of the control's control state (excluding child controls). For more information, seeASP.NET State Management. |
Session State
The Session State section displays information about values stored in Session state, if any. For more information, see ASP.NET Session State.
| Value | Description |
|---|---|
Session Key |
The key for data stored in session state, if any. |
Type |
The fully qualified type of the object that stores the data. |
Value |
A string representation of the data stored in session state, if any. |
Application State
The Application State section displays information about values stored in Application state, if any. For more information, seeASP.NET Application State.
| Value | Description |
|---|---|
Application Key |
The key for data stored in application state, if any. |
Type |
The fully qualified type of the object that stores the data. |
Value |
A string representation of the data stored in application state, if any. |
Cookies Collection
The Request Cookies and Response Cookies sections display information about the cookies passed between the browser and the server on each request and response. The section displays both persistent and session cookies. ASP.NET creates some cookies automatically, such as those for cookie-based Session state and forms authentication. For more information, seeASP.NET Cookies.
| Value | Description |
|---|---|
Name |
The name of the cookie. |
Value |
The value of the cookie, or subkeys and values if the cookie is multivalued. |
Size |
The size in bytes of the cookie. |
Headers Collection
The Headers Collection section displays information about request and response message header name/value pairs, which provide information about the message body or requested resource. Header information is used to control how request messages are processed and response messages are created. For more information on HTTP headers, see RFC 2616 at theWorld Wide Web Consortium (W3C) Web site.
| Value | Description |
|---|---|
Name |
The name of the header. |
Value |
The value of the header. |
Form Collection
The Form Collection section displays name/value pairs that show the form element values (control values) submitted in a request during a POST (postback).
| Value | Description |
|---|---|
Name |
The name of the form variable. |
Value |
The value of the form variable. |
Querystring Collection
The Querystring Collection section shows the values passed in the URL. In a URL, query string information is separated from the path information by a question mark (?); multiple query string elements are separated by an ampersand (&). Query string name/value pairs are separated by an equals sign (=). The NameValueCollection of query string variables.
| Value | Description |
|---|---|
Name |
The name of the query string variable. |
Value |
The value of the query string variable. |
Server Variables
The Server Variables section displays a collection of server-related environment variables and request header information. The ServerVariables property of the HttpRequest object returns a NameValueCollection of server variables.
| Value | Description |
|---|---|
Name |
The name of the server variable. |
Value |
The value of the server variable. |
有两种使用方式,一种是页面级的TRACE,一种是应用程序级。前者打开后直接在页面上输出TRACE信息,显然在生产环境中不适合。
后者可以配置不直接在页面输出而是通过TraceHandler(也就是Trace.axd)来查看:
<trace pageOutput="false" enabled="true" requestLimit="1" mostRecent="true"/>
我们可以配置内存中维护的队列长度也就是Trace的请求总数,也可以选择超过队列之后放弃最后的请求还是放弃最先的请求
这个Handler可以查看所有记录的请求也可以查看每一个请求的细节,也可以清空历史数据。
观察一下TraceHandler的源代码可以论证:
{
if (DeploymentSection.RetailInternal || (!context.Request.IsLocal && HttpRuntime.Profile.LocalOnly))
{
HttpException exception = new HttpException(0x193, null);
exception.SetFormatter(new TraceHandlerErrorFormatter(!DeploymentSection.RetailInternal));
throw exception;
}
this._context = context;
this._response = this._context.Response;
this._request = this._context.Request;
this._writer = Page.CreateHtmlTextWriterInternal(this._response.Output, this._request);
if (context.WorkerRequest is IIS7WorkerRequest)
{
this._response.ContentType = this._request.Browser.PreferredRenderingMime;
}
if (this._writer != null)
{
this._context.Trace.IsEnabled = false;
this._request.ValidateInput();
this._writer.Write("<html>\r\n");
this._writer.Write("<head>\r\n");
this._writer.Write(StyleSheet);
this._writer.Write("</head>\r\n");
this._writer.Write("<body>\r\n");
this._writer.Write("<span class=\"tracecontent\">\r\n");
if (!HttpRuntime.Profile.IsConfigEnabled)
{
HttpException exception2 = new HttpException();
exception2.SetFormatter(new TraceHandlerErrorFormatter(false));
throw exception2;
}
IList data = HttpRuntime.Profile.GetData();
if (this._request.QueryString["clear"] != null)
{
HttpRuntime.Profile.Reset();
string rawUrl = this._request.RawUrl;
this._response.Redirect(rawUrl.Substring(0, rawUrl.IndexOf("?", StringComparison.Ordinal)));
}
string s = this._request.QueryString["id"];
if (s != null)
{
int num = int.Parse(s, CultureInfo.InvariantCulture);
if ((num >= 0) && (num < data.Count))
{
this.ShowDetails((DataSet) data[num]);
this.ShowVersionDetails();
this._writer.Write("</span>\r\n</body>\r\n</html>\r\n");
return;
}
}
this.ShowRequests(data);
this.ShowVersionDetails();
this._writer.Write("</span>\r\n</body>\r\n</html>\r\n");
}
}