Monday, January 7, 2013

A Simple Text File logger for .Net apps

Sometimes you just need a simple kick start while debugging application. Once such scenario is to add simple file based logging. I have created a very simple file based Logger just to add some info to text file and comes handy when you actually want to look at the trace of events specially in a multi-threaded environment that is a little difficult to debug at runtime.

public class Logger
    {
        public static void Log(string str)
        {
            var file = Path.Combine(HttpContext.Current.Server.MapPath(@"~\"), "log.txt");
            System.IO.File.AppendAllText(file, string.Format("{0}-{1}{2}",DateTime.Now, str, Environment.NewLine));
        }
    }