How to Write Custom Tracing Message in ASP.Net

In this article I am explaining how to a write custom tracing message and what the difference is between Trace.Warn() and Trace.Write().

Background

When the application is deployed on the server then at that time if the application has some issue regarding security or something else then we have no mechanism to track the execution flow of ASP pages. So to know all the information we use Tracing.

What is tracing

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

Tracing hs 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 manipluate trace messages programitaclly.
  3. We can see the most recent tracing data.
  4. Tracing can be done from the following 2 levels:

    • Page Level
    • Application Level

I had explained both of them in my previous article.

Introduction to Tracing

How to Write custom Tracing Message

Step 1: Open Visual Studio 2010
Step 2: Then click on "New Project" > "Web" > "ASP.NET Empty Web Application".

Web Application

Step 3: Now go to the Solution Explorer.

Solution Explorer

Step 4: Now right-click on the solution in the Solution Explorer and go to "Add" > "New Item" > "Web Form" and add the name of the web form.

New Item

Step 5: Now go to the web form and for enable tracing at the page level we need to enable the trace at the page level and use some controls, 2 TextBoxes, a button and a label.

  1. <%@ Page Trace="true" Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Custom_Tracing_Message.WebForm1" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head id="Head1" runat="server">  
  6.     <title></title>  
  7. </head>  
  8. <body>  
  9.     <form id="form2" runat="server">  
  10.         <div>  
  11.             First Number   
  12.             <asp:TextBox ID="tbfirst" runat="server"></asp:TextBox>  
  13.             <br />  
  14.             Second Number   
  15.             <asp:TextBox ID="tbsecond" runat="server"></asp:TextBox>  
  16.             <br />  
  17.             <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add" />  
  18.             <br />  
  19.             <br />  
  20.             <asp:Label ID="Label1" runat="server"></asp:Label>  
  21.         </div>  
  22.     </form>  
  23. </body>  
  24. </html> 

Write the following code in code behind :

  1. protected void Button1_Click(object sender, EventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         //taking 2 variables and textbox values are stored in the variable  
  6.         int FirstNum = Convert.ToInt32(tbfirst.Text);  
  7.         int secondNum = Convert.ToInt32(tbsecond.Text);  
  8.         //add the variables and store in the result variable  
  9.        int result = FirstNum + secondNum;  
  10.        //adding value show in label  
  11.        Label1.Text = result.ToString();  
  12.     }  
  13.     catch (FormatException format)  
  14.     {  
  15.         Label1.Text = "Only Numbers are allowed";  
  16.         Trace.Write(format.Message);  
  17.     }  
  18.     catch (OverflowException over)  
  19.     {  
  20.         Label1.Text = "Numbers are too larger";  
  21.         Trace.Warn(over.Message);  
  22.     }  

Output

  1. If there are no errors then the output will be:

    output

  2. If the input string is not in the correct format then that means it is not in integer format. So then to show the error message I used Trace.Write() and the output will be:

    Trace.Write

  3. If the input string is in an overflow condition then to show the error message I used the Trace.Warn() method and the output will be:

    output

Conclusion

  1. The difference between the methods is that the Trace.warn() method shows the error message in Red color and the Trace.Write() method shows the error message in Black color.
  2. The Custom Tracing method helps to show the error messages when the site is deployed; we can easily get the error.

Up Next
    Ebook Download
    View all
    Learn
    View all