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

Logging exceptions as information entry type in windows eventviewer - Part 74

Suggested Videos
Part 71 - Custom errors
Part 72 - Windows event viewer
Part 73 - Logging exceptions to the windows eventviewer

In this video we will discuss about logging exceptions as Information entry type in windows event viewer. Before continuing with this session please watch Part 72 and Part 73.



Create an asp.net web application and add a class file with name Logger.cs.
public class Logger
{
    public static void Log(Exception exception)
    {
        Log(exception, EventLogEntryType.Error);
    }
    public static void Log(Exception exception, EventLogEntryType eventLogEntryType)
    {
    do
    {
        sbExceptionMessage.Append("Exception Type" + Environment.NewLine);
       sbExceptionMessage.Append(exception.GetType().Name);
        sbExceptionMessage.Append(Environment.NewLine + Environment.NewLine);
       sbExceptionMessage.Append("Message" + Environment.NewLine);
        sbExceptionMessage.Append(exception.Message + Environment.NewLine + Environment.NewLine);
       sbExceptionMessage.Append("Stack Trace" + Environment.NewLine);
        sbExceptionMessage.Append(exception.StackTrace + Environment.NewLine + Environment.NewLine);

       exception = exception.InnerException;
}
    while (exception != null);

        // If the Event log source exists
        if (EventLog.SourceExists("PragimTech.com"))
        {
            // Create an instance of the eventlog
            EventLog log = new EventLog("PragimTech");
            // set the source for the eventlog
            log.Source = "PragimTech.com";
            // Write the exception details to the event log as an error
            log.WriteEntry(sbExceptionMessage.ToString(), eventLogEntryType);
        }
    }
}



Add a webform, with name Calculator.aspx and copy the following 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>

Calculator.aspx.cs code:
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)
    {
        Logger.Log(formatException, EventLogEntryType.Information);
        lblMessage.ForeColor = System.Drawing.Color.Red;
        lblMessage.Text = "Only numbers are allowed";
    }
    catch (OverflowException overflowException)
    {
        Logger.Log(overflowException, EventLogEntryType.Information);
        lblMessage.ForeColor = System.Drawing.Color.Red;
        lblMessage.Text = "Numbers must be between " + Int32.MinValue.ToString() + " and " + Int32.MaxValue.ToString();
    }
    catch (DivideByZeroException divideByZeroException)
    {
        Logger.Log(divideByZeroException, EventLogEntryType.Information);
        lblMessage.ForeColor = System.Drawing.Color.Red;
        lblMessage.Text = "Denominator cannot be ZERO";
    }
    catch (Exception exception)
    {
        Logger.Log(exception);
        lblMessage.ForeColor = System.Drawing.Color.Red;
        lblMessage.Text = "An unknown problem has occured. Please try later";
    }
}

FormatException, OverflowException, DivideByZeroException should now be logged as "Information" entry type instead of "Error" entry type in the event viewer.

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.