CSharp examples for System:Exception
Saves information about exceptions which occured to the log file.
using System.Text; using System.Linq; using System.IO;/*from w w w . j a v a 2 s . c o m*/ using System.Collections.Generic; using System; public class Main{ /// <summary> /// Saves information about exceptions which occured to the log file. /// </summary> /// <param name="e">Exception which occured.</param> /// <param name="module">String with info about function and source code file.</param> public static void saveExceptionToLog(Exception e, string module) { DateTime failTime = DateTime.Now; string previousLogContent; // Trying to open exist log file and copy it contents. try { StreamReader fileContentCopy = new StreamReader("Logs/" + failTime.ToShortDateString() + ".log"); previousLogContent = fileContentCopy.ReadToEnd(); fileContentCopy.Close(); } catch { previousLogContent = ""; } // Recreating log contents and adding new enter. StreamWriter logSaver = new StreamWriter("Logs/" + failTime.ToShortDateString() + ".log"); logSaver.WriteLine(previousLogContent); logSaver.WriteLine(failTime.ToLocalTime().ToLongTimeString() + " - exception occurs in module: " + module); logSaver.WriteLine("Error message: " + e.Message + "\n"); logSaver.WriteLine("Error's data: " + e.Data); logSaver.WriteLine("Source: " + e.Source); logSaver.Close(); } }