if (!File.Exists(path)) { File.Create(path).Close(); } using (StreamWriter w = File.AppendText(path)) { w.WriteLine("Error log:"); w.WriteLine("Error Time:"+DateTime.Now); w.WriteLine("Error TargetSite:" + error.TargetSite); w.WriteLine("Error Message:" + error.Message); w.WriteLine("Error StackTrace" + error.StackTrace); w.WriteLine("-----------------------------------------------------------------------------------------------------------------------"); w.Flush(); w.Close(); }