SIGN UP MEMBER LOGIN:    
ARTICLE

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

Posted by Jawed MD Articles | .NET 4.5 February 15, 2012
The title sounds interesting, right? So, in this article we are going to discuss custom error reporting in an ASP.Net application.
Reader Level:
Download Files:
 

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!!

Login to add your contents and source code to this article
share this article :
post comment
 

how to I get page url from which page (refferer) it come to my customErrors page

Posted by priest ja Mar 04, 2012

Hey Laura, Thanks for your comments. and nice to hear that it was useful for you..

Posted by Jawed MD Feb 18, 2012

Hi Daisy, Thanks!! I m glad that some way its useful for you..

Posted by Jawed MD Feb 18, 2012

Hi Nitin, Thank you very much for your valuable comments!!!

Posted by Jawed MD Feb 18, 2012

Thank you, It is useful for me

Posted by Laura Parker Feb 15, 2012
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor