In ASP.NET application we need to consider below factors while creating error handling ability in system

- Show User friendly message in UI
- Log all detail level error
- Log error process should be very simple and generic so there are minimum chances of the error comes in log generation process and that should be easy to accessible to each user.

i.e. if we are storing login in database and error comes related to database connectivity then we never get any kind of logs and also that is difficult to retrieve that log

Show User Friendly message in UI:


Add below tags in your web.config file



Also need to add Errorpage.aspx and pagenotfound.aspx(if required) with user friendly messages.

Log all detail level error

For maintain each error log we need to add below code in global.asax page in Application_Error event.


void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError().GetBaseException();
string PageUrl = Request.Url.ToString();

string strLogFile = Server.MapPath(@"~\ErrorLog\LogFile_" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + ".txt");
ErrorHnadling.ErrorHandling(ex, strLogFile, Request.Form.ToString(), PageUrl);
}

So when ever error occurs this event fires and error is logged.


Error Log Process

We have implemented error log process in Errorhadling class with errorhandling methods with all error details.

We are storing error in text file with predefine web root folder with todays date as naming convention so any time user can access that file data by browsing it by web directory (with predefine name by date)

Below are the class for error handling.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
/* Created By : Amit Patel
* Created Date : 21th Feb 2011
* Description : Error Handling for application level
*/
namespace Common
{
public class ErrorHnadling
{
///
/// Log Exception Log file (log file is maintain based on Date
///

/// Exception Message
/// Log File
/// Request Form name
/// Query String Name
public static void ErrorHandling(Exception ex, string strLogFile, string strFormName, string strQueryString)
{

StreamWriter oSW;
if (File.Exists(strLogFile))
{
oSW = new StreamWriter(strLogFile, true);
}
else
{
oSW = File.CreateText(strLogFile);
}

oSW.WriteLine("================================" + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() + "================================");
oSW.WriteLine("MESSAGE : " + ex.Message);
oSW.WriteLine("SOURCE : " + ex.Source);
oSW.WriteLine("TARGETSITE : " + ex.TargetSite);
oSW.WriteLine("STACKTRACE : " + ex.StackTrace);
oSW.WriteLine("INNEREXCEPTION : " + ex.InnerException);
oSW.WriteLine("FORM : " + strFormName);
oSW.WriteLine("QUERYSTRING : " + strQueryString);
oSW.Close();
}
///
/// Log Exception Log file (log file is maintain based on Date
///

/// Exception Message
/// Log File
/// Request Form name
/// Query String Name
/// Data for Error Handling (user id or any other session variable
public static void ErrorHandling(Exception ex, string strLogFile, string strFormName, string strQueryString, string strData)
{

StreamWriter oSW;
if (File.Exists(strLogFile))
{
oSW = new StreamWriter(strLogFile, true);
}
else
{
oSW = File.CreateText(strLogFile);
}

oSW.WriteLine("================================" + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() + "================================");
oSW.WriteLine("MESSAGE : " + ex.Message);
oSW.WriteLine("SOURCE : " + ex.Source);
oSW.WriteLine("TARGETSITE : " + ex.TargetSite);
oSW.WriteLine("STACKTRACE : " + ex.StackTrace);
oSW.WriteLine("INNEREXCEPTION : " + ex.InnerException);
oSW.WriteLine("FORM : " + strFormName);
oSW.WriteLine("QUERYSTRING : " + strQueryString);
oSW.WriteLine("Data : " + strData);
oSW.Close();
}
}
}

Also we have added another method to capture application/session related data. That will help if any specific error comes on special data or specific user.

Hope you will enjoy with code to trace any error in production application.