Monday, May 7, 2007

Handling Errors for asp.net application

To CREATE ERROR LOG FOR APPLICATION -:

1)create ErrorLog.txt in web application directory
2)create a cs file in project and copy this class


public class ErrorDesc
{
private string sLogFormat;
private string sErrorTime;
public ErrorDesc()
{
sLogFormat = DateTime.Now.ToShortDateString().ToString()+" "+DateTime.Now.ToLongTimeString().ToString()+" ==> ";
//this variable used to create log filename format "
//for example filename : ErrorLogYYYYMMDD
string sYear = DateTime.Now.Year.ToString();
string sMonth = DateTime.Now.Month.ToString();
string sDay = DateTime.Now.Day.ToString();
sErrorTime = sYear+sMonth+sDay;
}
public void ErrorLog(string sPathName, string sErrMsg)
{
StreamWriter sw = new StreamWriter(sPathName,true);
sw.WriteLine(sLogFormat + sErrMsg);
sw.Flush();
sw.Close();
}


now whenever in web page we want to handle try catch use :

try{
*******code****
}

catch(Exception ex)
{
ErrorDesc err=new ErrorDesc();
err.ErrorLog(Server.MapPath("ErrorLog.txt"),ex.Message+ ex.StackTrace);
}

No comments: