"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Thursday, January 6, 2011

Maintaining error log

Here I am simplfying error log implementaiton.
To maintain error log its good to have log for each day for a better review .
so here we will create log file everyday , and when file already exist will append error entry in to it.
For that create one class and write following method....

C#
Add this 2 Namespace
using System.IO;

using System.Globalization;

and the Rest of game will be handled by this class

public static void WriteError(string errorMessage)
    {
        try
        {
            //Specify path where log file to be created
            string logfilePath = "~/Error/" + DateTime.Today.ToString("dd-mm-yy") + ".txt";
            if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(logfilePath)))
            {
                File.Create(System.Web.HttpContext.Current.Server.MapPath(logfilePath)).Close();
            }
            using (StreamWriter wrtr = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(logfilePath)))
            {
                wrtr.WriteLine("\r\nLog Entry : ");
                wrtr.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
                string err = "Error in: " + System.Web.HttpContext.Current.Request.Url.ToString() +
                ". Error Message:" + errorMessage;
                wrtr.WriteLine(err);
                wrtr.WriteLine("__________________________");
                wrtr.Flush();
                wrtr.Close();
            }
        }
        catch (Exception ex)
        {
            WriteError(ex.Message);
        }
    }





Have a nice day... 'N happy Coding :)

No comments: