How to create log file to record errors and exceptions in

IntroductionIn previous articles i explained with example How to Get city, state and country based on zip code using Google map API in , Maintain image on PostBack in FileUpload control, Count and display remaining characters in the multiline textbox in, Add meta description,meta keywords tags from code behind in and Create XML file and Bind XML data to CheckBoxList using DataSet. 
Now In this post I will explain with example How to create log file to log/record errors i.e. exceptions in Occurring errors i.e.exceptions are very common on the web site. But it is recommended to the developers to create a log file to record what error occurred and on what page the error occurred so that they can find and  resolve the error easily.

create log file to record errors and exceptions in
Click on image  to enlarge

  • Create a new website in visual studio and go to website menu ->Add new item ->  Select Global Application Class as shown in figure below.
  • Now in the Application_Error event in Global.asax file  write the code to log the error as:

void Application_Error(object sender, EventArgs e)
        Exception ex = new Exception();
        ex = Server.GetLastError().GetBaseException();

        System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/errorLOG.txt"), System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);
        System.IO.StreamWriter s = new System.IO.StreamWriter(fs);
        s.BaseStream.Seek(0, System.IO.SeekOrigin.End);
        s.WriteLine("ERROR DATE: " + System.DateTime.Now.ToString(System.Globalization.CultureInfo.InvariantCulture) + " \nERROR MESSAGE: " + ex.Message + "\nSOURCE: " + ex.Source + "\nFORM NAME: " + System.Web.HttpContext.Current.Request.Url.ToString() + "\nQUERYSTRING: " + Request.QueryString.ToString() + "\nTARGETSITE: " + ex.TargetSite.ToString() + "\nSTACKTRACE: " + ex.StackTrace + System.Diagnostics.EventLogEntryType.Error);
  •  Now whenever any error occur in your application it will record the error in the automatically create text file named errorLOG.txt
  • To check , let's generate an error.

C#.NET Code to create log file to record errors and exceptions
  • In the Page_Load event of any page e.g. default.aspx.cs write the following code as:

protected void Page_Load(object sender, EventArgs e)
        int a=10;
        int b=0;
        int c;     
        c = a / b;

VB.NET Code to create log file to record errors and exceptions

  • In the Page_Load event of any page e.g. default.aspx.vb write the following code as:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim a As Integer = 10
        Dim b As Integer = 0
        Dim c As Integer
        c = a \ b
    End Sub

It will generate an error and that error will be recorded in the errorLOG.txt text file  in the root directory of our website. Open the errorLOG.txt text file and see the results like shown in figure.

create log file to record errors and exceptions in

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..