How do I do logging in C# without using 3rd party libraries?

IAdapter picture IAdapter · Feb 20, 2011 · Viewed 209.7k times · Source

I would like to implement logging in my application, but would rather not use any outside frameworks like log4net.

So I would like to do something like DOS's echo to a file. What is the most effective way to do it?

Is there a way to log unhandled exceptions logged without using an outside framework?

Answer

Brandon picture Brandon · Feb 20, 2011
public void Logger(string lines)
{
  //Write the string to a file.append mode is enabled so that the log
  //lines get appended to  test.txt than wiping content and writing the log

  using(System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt", true))
  {
    file.WriteLine(lines);
  }
}

For more information MSDN