First we want to create a path to which file will be saved. For security reasons we want to save the file in App_Data folder so it won’t be accessible outside the application.
var filename = AppDomain.CurrentDomain.BaseDirectory + "App_Data\\" + "log\\" + "logErrors.txt";
Now we want to write some errors on throw exception. By doing that we have overall idea what could have gone wrong when app went live. Being a good programmer means that we should never have any errors in our application, but if something goes terribly wrong at least we have some info what happened.
var sw = new System.IO.StreamWriter(filename, true);
sw.WriteLine(DateTime.Now.ToString() + " " + e.Message + " " + e.InnerException );
// Silently fail if the write doesn't happen.