Tracing in ASP.NET

In the following article we will see how to use Tracing in ASP.NET.

While developing ASP.NET applications we often need to identify the problem in the application. Tracing can help us identify and resolve the issues in the application.

Tracing in ASP.NET is used to follow the page's execution path and allows us to view request-related information that helps us debug the problems and perform other optimizations. When the tracing is enabled we can view the trace information, either on a web page or using the trace viewer. Tracing information includes details such as Request, Response, Session, Controls and other useful information.

To enable page-level tracing we set the Trace attribute in the @Page directive to true. We can enable tracing at the application level by setting the trace element's enabled attribute to true in web.config. The trace information is appended to the bottom of the page that we can view when we request the page. We can also use the traceviewer (trace.axd) to view the trace information. The following setting enables the application-level tracing.

 

  1. <system.web>  
  2. <trace enabled="true" requestLimit="20" localOnly="true" />  
  3. </system.web>

We have set the localOnly to true so we can view the trace information only from the local machine on which the application is hosted. It is important to set localOnly to true if we do not want remote users to view the trace information.

We can also write our custom trace information to the page output or the trace log. We can append trace information using the Warn or Write methods of the TraceContext class.

The following line appends the text to the trace output:

Trace.Warn("appending the text to the trace output");

Both the page and the trace viewer displays the same information. The trace information is organized into tables. The following is the trace output displayed using the trace viewer:

Tracking1.jpg

The following are some of the main sections displayed for the trace output:

Tracking2.jpg

We can configure the tracing using the attributes of the trace element in the web.config file. The following are some of the main attributes of the trace element.

Enabled: We set this attribute to true to enable application-level tracing. We can override tracing for the individual pages using the trace attribute of the @Page directive in the individual pages.

PageOutput: We set this to true to display trace output in the page and the traceviewer(trace.asxd). The default value is false.

RequestLimit: The total number of requests to store on the server. The default value is 10. If the request exceeds this limit then it is not displayed in the trace output.

LocalOnly: Specifies if the TraceViewer is available on the local or the remote machine. The default is true.

So using tracing we can identify and resolve the problems in the application that are difficult to locate.


Similar Articles