Home > ASP.Net > Create An Error Log File

Create An Error Log File


Hi all,

Try this example to Create an error log file

Step 1: Add a Global.asax file to Project/website if not exist.

Step 2: Now add the namespace to Global.asax file.

<%@ Import Namespace="System.IO" %>

Step 3: After that write the below given code in Application_Error event of Global file:

void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception ex = new Exception();
ex = Server.GetLastError().GetBaseException();
FileStream fs = new FileStream(Server.MapPath("~/ErrorLOG.txt"), FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, System.IO.SeekOrigin.End);
sw.WriteLine("ERROR DATE: " + System.DateTime.Now.ToString(System.Globalization.CultureInfo.InvariantCulture));
sw.Write("\r\n");
sw.Write("ERROR MESSAGE: " + ex.Message);
sw.Write("\r\n");
sw.Write("SOURCE: " + ex.Source);
sw.Write("\r\n");
sw.Write("FORM NAME: " + System.Web.HttpContext.Current.Request.Url.ToString());
sw.Write("\r\n");
sw.Write("QUERYSTRING: " + Request.QueryString.ToString());
sw.Write("\r\n");
sw.Write("TARGETSITE: " + ex.TargetSite.ToString());
sw.Write("\r\n");
sw.Write("STACKTRACE: " + ex.StackTrace + System.Diagnostics.EventLogEntryType.Error);
sw.Write("\r\n");
sw.WriteLine("-----------------------------------------------------------------------------------------");
sw.Close();
}

Hope this helps

Good Luck.

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: