Thursday, August 12, 2010

Brain-dead simplistic logger

var message = "message that gets logged goes here";
System.IO.File.AppendAllText(System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), "log.txt"), string.Format("{0}\r\n", message));



Then type:
    %localappdata%
from the Run dialog to display the folder containing log.txt.
Or type:
    %localappdata%\log.txt

Now make a method out of it

....and have work like the Format command:

private static void Log(string format, params object[] parameters)
{
System.IO.File.AppendAllText(System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), "log.txt"), string.Format(format + "\r\n", parameters));
}

If you want to clear the log file, put this in the appropriate place:

System.IO.File.Delete(System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), "log.txt"));