Snippet

For a small text project I wanted to write an exception to a reusable text file. Each new exception should override the prior one. So, the file should hold just the last exception at any point in time.

Here is the code:

using System.IO;
...

string file = @"C:\Error.txt";

using (var writer = new StreamWriter(file))
{
    writer.WriteLine("Error Date :" + DateTime.Now.ToString() + Environment.NewLine + 
                     "Error Message :" + ex.Message + Environment.NewLine + 
                     "StackTrace :" + ex.StackTrace + Environment.NewLine);
}

Hopefully this is helpful to someone.