Introduction to Tracing in .Net

Background

When an application is deployed onto the server and if at that time the application has some issues regarding security and other then at that time we have no mechanism to track the execution flow of ASP pages. So for knowing all the information we use Tracing.

Introduction to Tracing

Tracing is introduced in the .NET 2.0 version. Tracing helps to see the information of issues at the runtime of the application. By default Tracing is disabled.

Tracing has the following important features:

  1. We can see the execution path of the page and application using the debug statement.
  2. We can access and manipulate trace messages programmatically.
  3. We can see the most recent tracing of the data.

Tracing can be done with the following 2 types.

1. Page Level: When the trace output is displayed on the page and for the page-level tracing we need to set the property of tracing at the page level.

  1. <%@ Page Trace="true" Language="C#" 

Now I am explaining Tracing with an example.

Step 1: Open Visual Studio 2010.

Step 2: Then click on "New Project" > "Web" > "ASP.NET Empty Web Application".

Web Application

Step 3: Now click on Solution Explorer and here click on "Add" > "New Item" > "Web Form" and add the name of the web form.

Web Form

Step 4: Now in the web form take the grid view control and bind with the database table:

  1. <%@ Page Trace="true" Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Tracing.WebForm1" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4.     <head runat="server">  
  5.        <title></title>  
  6.     </head>  
  7. <body>  
  8. <form id="form1" runat="server">  
  9. <div>  
  10.    <asp:GridView ID="GridView1" runat="server" BackColor="White"  
  11.     BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3"  
  12.     ForeColor="Black" GridLines="Vertical">  
  13.     <AlternatingRowStyle BackColor="#CCCCCC" />  
  14.     <FooterStyle BackColor="#CCCCCC" />  
  15.     <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />  
  16.     <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />  
  17.     <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />  
  18.     <SortedAscendingCellStyle BackColor="#F1F1F1" />  
  19.     <SortedAscendingHeaderStyle BackColor="#808080" />  
  20.     <SortedDescendingCellStyle BackColor="#CAC9C9" />  
  21.     <SortedDescendingHeaderStyle BackColor="#383838" />  
  22.    </asp:GridView>  
  23.     </div>  
  24.   </form>  
  25.  </body>  
  26. </html> 

Database table

Database
Code behind
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     //bind the grid view  
  4.     //connection string  
  5.     string con = "server=divs\\sqlexpress;database=pragim;uid=sa;password=divya";  
  6.     SqlConnection con1 = new SqlConnection(con);  
  7.     //write query  
  8.     string query = "select * from student_info";  
  9.     SqlDataAdapter da = new SqlDataAdapter(query, con1);  
  10.     DataSet ds = new DataSet();  
  11.     //fill in dataset  
  12.     da.Fill(ds);  
  13.     GridView1.DataSource = ds;  
  14.     GridView1.DataBind();  

Output

Output

2. Application-Level: In Application-Level tracing the information is stored for each request of the application. The default number of requests to store is 10. But if you want to increase the number of requests and discard the older request and display a recent request then you need to set the property in the web.config file.
  1. <trace enabled="true"/> 
Output

The output will show the trace.axd file when we use Application-Level tracing.

application level tracing

Attribute of trace tag
  1. Enabled: Enable or Disable tracing for the entire application.
  2. PageOutput: If this attribute value is set to “true” then the trace output will be displayed on the page. If it is false then we need to use the trace.axd file for viewing the tracing information.
  3. RequestLimit: This attribute defines the number of page requests that are to be traced.
  4. TraceMode: This attribute shows the order of the trace like SortByTime, SortByCategory and so on.
  5. LocalOnly: If you want to access the information on the local machine then set it to "True".
  6. MostRecent: If it is true then it displays the recent information and discards the oldest information.

Trace Information: Trace information is displayed as a table format in the following order:

  1. Request Details: In this section the information is displayed about the request and response.
     
     Value Description
     Session Id It binds with the browser for a specific purpose
     Time of request When the page is sent to the server
     Request Encoding The character encoding for the request
     Request type The type of request that is Get/Post
     Status Code It is associated with the response
     Response Encoding The character encoding for the response

  2. Trace Information: The Trace Information section displays the flow of page-level events. Custom trace messages are displayed in this section only.
  3. Control Tree: This section displays the controls of the ASP.NET page.
  4. Session State: This section displays the session values.
  5. Application Stat: This section displays the value stored in the application state.
  6. Cookies Collection: This section displays the information about cookies that is passed between server and client.
  7. Headers collection: This section displays information about request and response header name/value pair.
  8. Forma Collection: This section displays the form element value and it means that control values that are submitted during post-back.
  9. Querystring Collection: This section shows the Query string value that is passed in the URL.
  10. Server Variable: This section shows the server-related variable and information about the request.

Up Next
    Ebook Download
    View all
    Learn
    View all