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 occurrences 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 occur in the application and log the error details into a text file.
  1. public class ExceptionLog  
  2. {  
  3.     /// <summarry>  
  4.     /// The The following method helps in generating the Log file & Entries.  
  5.     /// </summarry>  
  6.     /// <param name="ApplicationException">Previous Exception that is used for entries in Log file</param>  
  7.    
  8.     public static void MessageDetails(Exception ApplicationException)  
  9.     {  
  10.         string strLogFileDetails = (HttpContext.Current.Server.MapPath("~/ErrorLog.txt"));  
  11.         Exception exception = ApplicationException;  
  12.         exception = (exception.InnerException != null ? exception.InnerException : exception);  
  13.         exception = (exception.InnerException != null ? exception.InnerException : exception);  
  14.         exception = (exception.InnerException != null ? exception.InnerException : exception);  
  15.         string sLogEntryFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString();  
  16.         using (StreamWriter Sw = new StreamWriter(strLogFileDetails, true))  
  17.         {  
  18.             Sw.WriteLine("Date       :   " + sLogEntryFormat);  
  19.             Sw.WriteLine("Message    :   " + exception.Message);  
  20.             Sw.WriteLine("Machine    :   " +  
  21.                     HttpContext.Current.Request.UserHostAddress);  
  22.             Sw.WriteLine("Source     :   " + exception.Source);  
  23.             Sw.WriteLine("StackTrace :   " + exception.StackTrace);  
  24.             Sw.WriteLine("----------------------------------------------------------------------------------------------------------------------");  
  25.             Sw.Flush();  
  26.         }  
  27.     }  
  28. }  
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 an 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.
  1. /// <summary>  
  2. /// fired when an error occurs.  
  3. /// </summary>  
  4. /// <param name="sender"></param>  
  5. /// <param name="e"></param>  
  6. protected void Application_Error(object sender, EventArgs e)  
  7. {  
  8.     System.Web.HttpContext context = HttpContext.Current;  
  9.     System.Exception ex = Context.Server.GetLastError();  
  10.     ExceptionLog.MessageDetails(ex);  
  11. }  
Hope it will be useful for you!!
 
Feel free to provide your comments and any suggestions!!


Similar Articles