Step 1: catch your exceptions
catch (Exception ex)
{
ErrorHandler e2 = new ErrorHandler();
e2.HandleException(ex);
}
where ErrorHandler is a class name and HandleException is method name where pe pass this exception.
Step 2: Create a seperate class file for handling all types of exception and also for generating trace or error log file.
public void HandleException(Exception exceptionName)
{
string ExpName = Convert.ToString(exceptionName.GetType().FullName.Remove(0,7));
switch (ExpName)
{
case "Data.SqlClient.SqlException":
WriteError(exceptionName);
System.Web.HttpContext.Current.Response.Redirect("ErrorPage.aspx");
break;
case "ArgumentException":
WriteError(exceptionName);
System.Web.HttpContext.Current.Response.Redirect("ErrorPage.aspx");
break;
case "FieldAccessException":
WriteError(exceptionName);
System.Web.HttpContext.Current.Response.Redirect("ErrorPage.aspx");
break;
// you can search aand so many exception here
default:
WriteError(exceptionName);
System.Web.HttpContext.Current.Response.Redirect("ErrorPage.aspx");
break;
}
}
where WriteError is method name where we pass the exception obj and retrive all information through this method
Step 3:
write the definition of WriteError method
private void WriteError(Exception errorMessage)
{
string path = "~/Error/" + DateTime.Today.ToString("dd-mm-yy") + ".txt";
if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
{
File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
}
using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
{
w.WriteLine("\r\nLog Entry : ");
string err = "Error in: " + System.Web.HttpContext.Current.Request.Url.ToString() + ". Error Message:" + errorMessage.Message;
string err1 = "Error Type:" + errorMessage.GetType();
string err2 = "Error BaseException:" + errorMessage.GetBaseException();
string err4 = "Error StackTrace:" + errorMessage.StackTrace;
w.WriteLine("................");
w.WriteLine(err);
w.WriteLine("................");
w.WriteLine(err1);
w.WriteLine("................");
w.WriteLine(err2);
w.WriteLine("................");
w.WriteLine(err4);
w.WriteLine("__________________________");
w.Flush();
w.Close();
}
}
and also add these namespace
using System.IO;
using System.Text;
catch (Exception ex)
{
ErrorHandler e2 = new ErrorHandler();
e2.HandleException(ex);
}
where ErrorHandler is a class name and HandleException is method name where pe pass this exception.
Step 2: Create a seperate class file for handling all types of exception and also for generating trace or error log file.
public void HandleException(Exception exceptionName)
{
string ExpName = Convert.ToString(exceptionName.GetType().FullName.Remove(0,7));
switch (ExpName)
{
case "Data.SqlClient.SqlException":
WriteError(exceptionName);
System.Web.HttpContext.Current.Response.Redirect("ErrorPage.aspx");
break;
case "ArgumentException":
WriteError(exceptionName);
System.Web.HttpContext.Current.Response.Redirect("ErrorPage.aspx");
break;
case "FieldAccessException":
WriteError(exceptionName);
System.Web.HttpContext.Current.Response.Redirect("ErrorPage.aspx");
break;
// you can search aand so many exception here
default:
WriteError(exceptionName);
System.Web.HttpContext.Current.Response.Redirect("ErrorPage.aspx");
break;
}
}
where WriteError is method name where we pass the exception obj and retrive all information through this method
Step 3:
write the definition of WriteError method
private void WriteError(Exception errorMessage)
{
string path = "~/Error/" + DateTime.Today.ToString("dd-mm-yy") + ".txt";
if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
{
File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
}
using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
{
w.WriteLine("\r\nLog Entry : ");
string err = "Error in: " + System.Web.HttpContext.Current.Request.Url.ToString() + ". Error Message:" + errorMessage.Message;
string err1 = "Error Type:" + errorMessage.GetType();
string err2 = "Error BaseException:" + errorMessage.GetBaseException();
string err4 = "Error StackTrace:" + errorMessage.StackTrace;
w.WriteLine("................");
w.WriteLine(err);
w.WriteLine("................");
w.WriteLine(err1);
w.WriteLine("................");
w.WriteLine(err2);
w.WriteLine("................");
w.WriteLine(err4);
w.WriteLine("__________________________");
w.Flush();
w.Close();
}
}
and also add these namespace
using System.IO;
using System.Text;
No comments:
Post a Comment