Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Writing custom asp.net tracing messages - Part 80

Suggested Videos
Part 77 - Sending emails using asp.net
Part 78 - Sending emails in asp.net using SMTP server settings from web.config
Part 79 - Tracing in asp.net

To write custom asp.net trace messages Trace.Write() and Trace.Warn() methods can be used. The only difference, between these 2 methods is that, the messages written using Trace.Warn() are displayed in red colour, where as the messages written using Trace.Write() are displayed in black colour. In fact, What is the difference between Trace.Write() and Trace.Warn() is a very common asp.net interview question.



Webform1.aspx HTML:
<div style="font-family: Arial">
    <table style="border: 1px solid black">
        <tr>
            <td>
                <b>First Number</b>
            </td>
            <td>
                <asp:TextBox ID="txtFirstNumber" runat="server">
                </asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <b>Second Number</b>
            </td>
            <td>
                <asp:TextBox ID="txtSecondNumber" runat="server">
                </asp:TextBox>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btnDivide" runat="server" Text="Divide" 
                OnClick="btnDivide_Click" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Label ID="lblMessage" runat="server" Font-Bold="true">
                </asp:Label>
            </td>
        </tr>
    </table>
</div>



WebForm1.aspx.cs code behind:
protected void btnDivide_Click(object sender, EventArgs e)
{
    try
    {
        int FirstNumber = Convert.ToInt32(txtFirstNumber.Text);
        int SecondNumber = Convert.ToInt32(txtSecondNumber.Text);

        lblMessage.ForeColor = System.Drawing.Color.Navy;
        int Result = FirstNumber / SecondNumber;
        lblMessage.Text = Result.ToString();
    }
    catch (FormatException formatException)
    {
        lblMessage.ForeColor = System.Drawing.Color.Red;
        lblMessage.Text = "Only numbers are allowed";
        // Check if tracing is enabled
        if (Trace.IsEnabled)
        {
            // Write the exception message to the trace log
            Trace.Write(formatException.Message);
        }
    }
    catch (OverflowException overflowException)
    {
        lblMessage.ForeColor = System.Drawing.Color.Red;
        lblMessage.Text = "Numbers must be between " + Int32.MinValue.ToString() + " and " + Int32.MaxValue.ToString();
        if (Trace.IsEnabled)
        {
            Trace.Warn(overflowException.Message);
        }
    }
    catch (DivideByZeroException divideByZeroException)
    {
        lblMessage.ForeColor = System.Drawing.Color.Red;
        lblMessage.Text = "Denominator cannot be ZERO";
        if (Trace.IsEnabled)
        {
            Trace.Warn(divideByZeroException.Message);
        }
    }
    catch (Exception exception)
    {
        lblMessage.ForeColor = System.Drawing.Color.Red;
        lblMessage.Text = "An unknown problem has occured. Please try later";
        if (Trace.IsEnabled)
        {
            Trace.Warn(exception.Message);
        }
    }
}

Trace.IsEnabled property can be used to check if tracing is enabled. In the following code Trace.Write() method is invoked only if tracing is enabled. If you don't check, Trace.IsEnabled property prior to writing out trace messages, we don't get an exception. But if you are going to do any sort of significant work to build the trace message, then you can avoid that work by checking the IsEnabled property first.
if (Trace.IsEnabled)
{
    Trace.Write("Debugging information");
}

With classic ASP, the only option for printing debugging information is Response.Write(). There are 2 problems with this
1. Actual end users also, can see the debugging information that you have written using Response.Write(). But with tracing, if pageoutput attribute is set to false, then the trace messages are written to the trace.axd file. End users cannot see the trace information.
2. All the Response.Write() statements must be removed, before the application is deployed to production. But with tracing, all you have to do is disable tracing in web.config.

No comments:

Post a Comment

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.