Friday, July 4, 2008

Catching Errors in Event Log in root folder

Hello All,

This function helps you write the errors to event.log
which can be called in the catch block of try and catch.

public void CatchError(Exception objErr)

{
//Exception objErr = Server.GetLastError().GetBaseException(); --This is
//to be used in application_error and Page_error events


string err = "Error Caught in try and catch block \n" +
"Error in: " + Request.Url.ToString() +
"\nError Message:" + objErr.Message.ToString() +
"\nStack Trace:" + objErr.StackTrace.ToString() ;
err = err + System.DateTime.Today.ToString() +" " + txtPhoneNumber.Text +"\n";



// EventLog.WriteEntry("SAWeb", err, EventLogEntryType.Error); This s //to be used while writing to event log
//Server.ClearError();
// additional actions...
// TextWriter tw = new StreamWriter("C:/Inetpub/wwwroot/SAWeb/logs/error.log",true);

TextWriter tw = new StreamWriter(Request.PhysicalApplicationPath.ToString() + "logs\\error.log", true);
// write a line of text to the file
tw.WriteLine(err);

// close the stream
tw.Close();



}

No comments: