Tracing in ASP.NET


Introduction

 

[MSDN Definition] ASP.NET tracing enables you to view diagnostic information about a single request for an ASP.NET page. ASP.NET tracing enables you to follow a page's execution path, display diagnostic information at run time, and debug your application. ASP.NET tracing can be integrated with system-level tracing to provide multiple levels of tracing output in distributed and multi-tier applications.

 

There are two ways:

 

(i) Page Level Tracing

(ii) Application Level Tracing

 

 

Page Level Tracing

 

We can control whether tracing is enabled or disabled for individual pages. If tracing is enabled, when the page is requested, ASP.NET appends to the page a series of tables containing execution details about the page request. Tracing is disabled by default in an ASP.NET application.

 

To enable Page Level Tracing follow the steps:

 

i) Include Trace="true" in <%@ Page Title="" Language="C#"...%> directive, for example:

 

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs"Inherits="chat_Default" Trace="true"%>

 

Look at the above code, I'll be using Trace=true at the end.

 

ii) Optionally, we can use TraceMode attribute in above <%@ Page Title="" Language="C#"...%> directive, for example:

 

image002.jpg

 

SortByCategory: Set TraceMode to SortByTime to sort trace messages in the order in which they are processed.

 

SortByTime: Set TraceMode to SortByCategory to sort trace messages by the categories.

 

iii) Now press F5 to run the application, you will see the immediate trace record on an existing page that you have set Trace and TraceMode.

 

You can see the traced record in the Trace Viewer as well; you will learn this in 'Application Level Tracing'.

 

 

Application Level Tracing

 

Instead of enabling tracing for individual pages, you can enable it for your entire application. In that case, every page in your application displays trace information. Application tracing is useful when you are developing an application because you can easily enable it and disable it without editing individual pages. When your application is complete, you can turn off tracing for all pages at once.

 

When you enable tracing for an application, ASP.NET collects trace information for each request to the application, up to the maximum number of requests you specify. The default number of requests is 10. You can view trace information with the trace viewer.

 

By default, when the trace viewer reaches its request limit, the application stops storing trace requests. However, you can configure application-level tracing to always store the most recent tracing data, discarding the oldest data when the maximum number of requests is reached.

 

 

To enable Application Level Tracing follow the steps:

 

i) Delete your Page Level Tracking for better result.

 

ii) Open the Web.config file and add the following information to it; if there is not a Web.config file available then add a new one in the root.

 

  :::::::::::::::::

  <system.web>

    <trace enabled="true" pageOutput="true" requestLimit="40" localOnly="false"/>

  </system.web>

</configuration>

 

iii) The above code has many attributes used, find the detailed information about them below.

 

Enabled: Set it true to enable tracing for the application; otherwise, false. The default is false. You can override this setting for individual pages by setting the Trace attribute in the @ Page directive of a page to true or false.

 

PageOutput: Set it true to display trace both in pages and in the trace viewer (trace.axd); otherwise, false. The default is false.

 

RequestLimit: The number of trace requests to store on the server. The default is 10.

 

LocalOnly: Set it true to make the trace viewer (trace.axd) available only on the host Web server; otherwise, false. The default is true.

 

image004.jpg

 

 

What is trace.axd?

 

Simply, it is a viewer which has some features for displaying trace information for us. This file is not so necessary when you set PageOutput to true. To add this file to the application simply navigate to add new file and name it trace.axd; it will automatically add the required code for the viewer.

 

To read above traced information you need to learn more. Please read this.