Log error in a text file in ASP.Net

It’s good practice to log all the error into a file or database so in future you can check, how your application is working, and is there any issue which is not caught by Q.A. team.

Really it's helpful to understand and caught the issues after deploying the application on production server.

Create a method to log the error into a file:

public void LogError(Exception ex, String source)
       String LogFile = HttpContext.Current.Request.MapPath("/Errorlog.txt");
       if (LogFile != "")
           String Message = String.Format("{0}{0}=== {1} ==={0}{2}{0}{3}{0}{4}{0}{5}"
                    , Environment.NewLine
                    , DateTime.Now
                    , ex.Message
                    , source
                    , ex.InnerException
                    , ex.StackTrace);
           byte[] binLogString = Encoding.Default.GetBytes(Message);

           System.IO.FileStream loFile = new System.IO.FileStream(LogFile
                     , System.IO.FileMode.OpenOrCreate
                     , System.IO.FileAccess.Write, System.IO.FileShare.Write);
           loFile.Seek(0, System.IO.SeekOrigin.End);
           loFile.Write(binLogString, 0, binLogString.Length);
   catch { /*No need to catch the error here. */}

We are using two parameter 1. Exception and 2. String (source where we will pass the page url so we can understand on which page this error occured).

We are loging page URL, Error Message, InnerException and StackTrace. It's enough to understand the reason and page to fix the code.

You can create this method anywhere either in Business Logic Layer or on the Global.asax file itself.

Now open the Global.asax file and in Application_Error method add following lines

void Application_Error(object sender, EventArgs e) 
    // Code that runs when an unhandled error occurs
    Exception ex = Server.GetLastError();
    LogError(ex, HttpContext.Current.Request.Path);

Add a file on the root with name "Errorlog.txt" and give read and write permission to it so new error can be write in this file, on develpment machine no need to give the read write permission becuase it already have it.

Now your application will log every error in your application to a text file name "Errorlog.txt" on the root of the application.

Myghty .Net developer
  • asp.net
  • c#
By Myghty On 30 Dec, 12  Viewed: 1,947

Other blogs you may like

Readonly textbox postback issues and solutions

In many cases we need to use read only text box so user cannot edit the value but readonly textbox will lost its value after postback. Let’s say you have a read only text box to set date by using ajax calendar so after post back date will be lost, other case might be you are setting some value in... By Ali Adravi   On 24 Apr 2013  Viewed: 4,302

Call code behind method from JavaScript in asp.net

There are ways to call a web service method JavaScript, for more detail you can see [how to retrieve data from database using JavaScript in asp.net][1], but is there any way to call a normal method from JavaScript? And the answer is No; show how we can call a code behind method from JavaScript,... By Jonathan King   On 08 Apr 2013  Viewed: 12,506

Gridview paginated data with search and sort functionality in asp.net

Most of the times we need to use GridView control to show tabular data in our asp.net application. We simply write procedure to search the records and bind them with GridView and show 10 to 20 records per page. Have you ever thought that why you are fetching all the records and show only 10 to 20... By Ali Adravi   On 16 Feb 2013  Viewed: 8,569

ASP.Net 4.5 new feature Model Binding

A nice feature with ASP.Net is the model binding, it reduced our code and effort to bind our well know controls like GridView, DataList, Repeater etc. So let’s see how we can bind our old controls in new and easy way. Let’s see how we bind the our grid before 4.5 1. We write the code to get... By Mike .Net   On 17 Jan 2013  Viewed: 3,217

Upload multiple image in multiple size with progress bar in asp.net

In asp.net there is not control to select multiple files and upload them once with progress bar, so we will use a small third party DLL to achieve this functionality. We will use Flajaxian FileUploader, you can download it from [http://www.flajaxian.com][1] We will create three different images... By Hamden   On 12 Jul 2012  Viewed: 6,567