kasajian
5/20/2014 - 4:06 AM

Poor Man's C Sharp Logger

Poor Man's C Sharp Logger

Brain-dead simplistic logger

    var message = ".....";
    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

A method taking variable number of arguments

    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, use this in the appropriate spot:

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

logs the stack

    System.IO.File.AppendAllText(System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), "stacklog.txt"), string.Format("{0}\r\n", (new System.Diagnostics.StackTrace()).ToString()));