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.

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.

<%@ 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.

New Item

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

<%@ Page Trace="true" Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Tracing.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" BackColor="White"
                BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3"
                ForeColor="Black" GridLines="Vertical">
                <AlternatingRowStyle BackColor="#CCCCCC" />
                <FooterStyle BackColor="#CCCCCC" />
                <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#F1F1F1" />
                <SortedAscendingHeaderStyle BackColor="#808080" />
                <SortedDescendingCellStyle BackColor="#CAC9C9" />
                <SortedDescendingHeaderStyle BackColor="#383838" />
            </asp:GridView>
        </div>
    </form>
</body>
</html>

Database table

Database

Code behind

protected void Page_Load(object sender, EventArgs e)   
{   
    // bind the grid view   
    // connection string   
    string con = "server=divs\\sqlexpress;database=pragim;uid=sa;password=divya";   
    SqlConnection con1 = new SqlConnection(con);   
    // write query   
    string query = "select * from student_info";   
    SqlDataAdapter da = new SqlDataAdapter(query, con1);   
    DataSet ds = new DataSet();   
    // fill in dataset   
    da.Fill(ds);   
    GridView1.DataSource = ds;   
    GridView1.DataBind();   
} 

Output

Output

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.

<trace enabled="true"/>

Output

The output will show the trace. and 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 in 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 are passed between the server and the client.
  7. Headers collection: This section displays information about the request and response header name/value pair.
  8. Forma Collection: This section displays the form element value and it means that control values 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.


Similar Articles