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:
- We can see the execution path of the page and application using the debug statement.
- We can access and manipluate trace messages programitaclly.
- We can see the most recent tracing data.
- 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".
Step 3: Now go to the 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.
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.
- <%@ Page Trace="true" Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Custom_Tracing_Message.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 id="Head1" runat="server">
- <title></title>
- </head>
- <body>
- <form id="form2" runat="server">
- <div>
- First Number
- <asp:TextBox ID="tbfirst" runat="server"></asp:TextBox>
- <br />
- Second Number
- <asp:TextBox ID="tbsecond" runat="server"></asp:TextBox>
- <br />
- <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add" />
- <br />
- <br />
- <asp:Label ID="Label1" runat="server"></asp:Label>
- </div>
- </form>
- </body>
- </html>
Write the following code in code behind :
- protected void Button1_Click(object sender, EventArgs e)
- {
- try
- {
-
- int FirstNum = Convert.ToInt32(tbfirst.Text);
- int secondNum = Convert.ToInt32(tbsecond.Text);
-
- int result = FirstNum + secondNum;
-
- Label1.Text = result.ToString();
- }
- catch (FormatException format)
- {
- Label1.Text = "Only Numbers are allowed";
- Trace.Write(format.Message);
- }
- catch (OverflowException over)
- {
- Label1.Text = "Numbers are too larger";
- Trace.Warn(over.Message);
- }
- }
Output
- If there are no errors then the output will be:
- 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:
- 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:
Conclusion
- 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.
- The Custom Tracing method helps to show the error messages when the site is deployed; we can easily get the error.