Custom Error Reporting in ASP.NET Using C# .NET


The title sounds interesting, right? So, in this article we are going to discuss custom error reporting in an ASP.Net application.

We are going to use C#.Net code to trap and respond to errors when they occur in ASP.Net or rather I would say in our web application.

To trap the error occurances we have written a separate class with the name ExceptionLog.cs, and we are using Global.asax to know about the error and trap it through our ExceptionLog.cs class file and record the details into a text file.

ExceptionLog.cs file:

In the class file we will write our logic to trap the errors that occurr in the application and log the error details into a text file.

public class ExceptionLog

{

    /// <summarry>

    /// The The following method helps in generating the Log file & Entries.

    /// </summarry>

    /// <param name="ApplicationException">Previous Exception that is used for entries in Log file</param>

 

    public static void MessageDetails(Exception ApplicationException)

    {

        string strLogFileDetails = (HttpContext.Current.Server.MapPath("~/ErrorLog.txt"));

        Exception exception = ApplicationException;

        exception = (exception.InnerException != null ? exception.InnerException : exception);

        exception = (exception.InnerException != null ? exception.InnerException : exception);

        exception = (exception.InnerException != null ? exception.InnerException : exception);

        string sLogEntryFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString();

        using (StreamWriter Sw = new StreamWriter(strLogFileDetails, true))

        {

            Sw.WriteLine("Date       :   " + sLogEntryFormat);

            Sw.WriteLine("Message    :   " + exception.Message);

            Sw.WriteLine("Machine    :   " +

                    HttpContext.Current.Request.UserHostAddress);

            Sw.WriteLine("Source     :   " + exception.Source);

            Sw.WriteLine("StackTrace :   " + exception.StackTrace);

            Sw.WriteLine("----------------------------------------------------------------------------------------------------------------------");

            Sw.Flush();

        }
    }


The Log File:

As I said the error details will be logged in atextile with the name ErrorLog.txt

Date       :   2/14/2012 1:14:31 PM

Message    :   Value cannot be null.

Machine    :   jawed **.**.**.***
 

Source     :   MyWebApplication

StackTrace :      at MyWebApplication.Default.Page_Load(Object sender, EventArgs e) in D:\WorkSpace\Source\MyWebApplication\Default.aspx.cs:line 15

   at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)

   at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)

   at System.Web.UI.Control.OnLoad(EventArgs e)

   at System.Web.UI.Control.LoadRecursive()

   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

-----------------------------------------------------------------------------

Date       :   2/15/2012 3:14:40 PM

Message    :   Value cannot be null.

Machine    :   jawed **.**.**.***
Source     :   MyWebApplication

StackTrace :      at MyWebApplication.Default.Page_Load(Object sender, EventArgs e) in D:\WorkSpace\Source\MyWebApplication\Default.aspx.cs:line 15

   at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)

   at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)

   at System.Web.UI.Control.OnLoad(EventArgs e)

   at System.Web.UI.Control.LoadRecursive()

   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

--------------------------------------------------------------------------


Global.asax

We will use the benefit of Global.asax to call an error trap method upon any Application error.

To know more about the Global.asax file you can visit the following links:

1. http://en.wikipedia.org/wiki/Global.asax

2. http://msdn.microsoft.com/en-us/library/1xaas8a2(v=vs.71).aspx

3. http://www.dotnetcurry.com/ShowArticle.aspx?ID=126

4. http://support.microsoft.com/kb/306355

So here is the piece of code to catch the errors that occur in the application and log them into a text file through our custom error logged class.

/// <summary>

/// fired when an error occurs.

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

protected void Application_Error(object sender, EventArgs e)

{

    System.Web.HttpContext context = HttpContext.Current;

    System.Exception ex = Context.Server.GetLastError();

    ExceptionLog.MessageDetails(ex);

}

 
Hope it will be useful for you!!

Feel free to provide your comments and any suggestions!!

Up Next
    Ebook Download
    View all

    milton

    Read by 0 people
    Download Now!
    Learn
    View all