How can I easily log a message to a file in c#

When you want to write some text to a log file for quick debugging, you can use

System.IO.File.AppendAllText(@"D:\Temp\WriteLines.txt",
    string.Concat("\n", DateTime.Now, "Your message here",
        this.GetType().Name, ":",
        System.Reflection.MethodBase.GetCurrentMethod().Name));

You will have Datetime, Class and method names with your log message. Your file will be created if not exist and new log messages will be appended. Have fun finding that pesky event causing bug you are trying to solve!

Avatar
Bhargava Mummadireddy
Software Craftsman