Wednesday, January 16, 2008

Simple error log files in ASP .NET

In some circumstances, you may not be able to debug your web application to determine what exception had occurred in it. So to create an error log file is very important to keep track of the occurred exception.

In this post, I will show you how to write an error log files by using a class file (.cs).

First of all, a class called CreateLogFiles needs to be created. In this class file, add in the codes as following:

using System;
using System.IO;
using System.Text;

namespace CSharpWeb
{
    public class CreateLogFiles
    {
        private string sFormat;
        private string sTime;

        public CreateLogFiles()
        {
            sFormat = DateTime.Now.ToShortDateString().ToString()+" "+DateTime.Now.ToLongTimeString().ToString()+" --- ";
            sTime = DateTime.Now.ToString("yyyyMMdd");
        }

         public void CreateErrorLog(string strPath, string sErr)
        {
            StreamWriter sw = new StreamWriter(strPath+sTime+".txt",true);
            sw.WriteLine(sFormat + sErr);
            sw.Flush();
            sw.Close();
         }
     }
}
In your own web page, you can use this class to create error log files, namely whenever an exception is being caught, it will create an error log file with the exception error message.

The code below is an example of mine, which I will call the CreateErrorLog function when a Checking button is clicked.

private void btnCheck_Click(object sender, System.EventArgs e)
{
    try
    {
        // ...The rest of your codes here
    }
    catch(Exception ex)
    {
         CreateLogFiles Error = new CreateLogFiles();
         Error.CreateErrorLog(Server.MapPath("Logs/ErrorLog"), ex.Message);
    }
}
Fro the codes as above, I am using Try…Catch method. Whenever an exception occurs, it will get caught by the Exception method. In there, an error log files will be created and saved as a keep track.

That’s it. If you have any curious feel free to ask me. Cheers!

4 comments:

Inspiron on August 24, 2008 at 10:41 AM said...

Hi, what is Map Path in "Error.CreateErrorLog(Server.MapPath("Logs/ErrorLog"), ex.Message)"?

Can I create our own path for error log?

Can I update this error log? So that every time an error has occur, this error log will be updated. Is this possible?

Thank you. :)

ejuly on August 24, 2008 at 3:48 PM said...

Server.MapPath function converts a virtual path into its corresponding physical path and is typically used to read or modify physical files on the server in the context of a Web request.

Sure. You can create your own path for this error log.

And yes again, you can update this error log whenever an error has occur, as the main purpose that i want to show you in this post is to keep track of the occurred exception and update to the error log.

life on October 2, 2009 at 9:15 PM said...

thank you yar.............
its best code..
solve my many problems

life on October 2, 2009 at 9:16 PM said...

thanx its solve my many problem...

 

Get paid for your opinions! Click on the banner above to join Planet Pulse. Its totally free to sign up, and you can earn UNLIMITED. Find out more by visiting PLANET PULSE.
July Code Blog Copyright © 2010 Blogger Template Designed by Bie Blogger Template