Writes an exception to the log file, you can know which exceptions the program has occurred while debugging the program, and you can know where the exception occurred. This is especially effective for programs that need to be run long and debugged.
1 /// <summary>2 ///print an exception to the log file3 /// </summary>4 /// <param name= "ex" >Exception</param>5 /// <param name= "logaddress" >log file Address</param>6 Public Static voidWritelog (Exception ex,stringLogaddress ="")7 {8//If the log file is empty, the new Yyyy-mm-dd_log.log file is created by default in the Debug directory9if(Logaddress = ="")Ten { Onelogaddress = Environment.currentdirectory +'\\'+ ADateTime.Now.Year +'-'+ -DateTime.Now.Month +'-'+ -DateTime.Now.Day +"_log.log"; the } -//output exception information to a file -StreamWriter fs =NewStreamWriter (Logaddress,true); -Fs. WriteLine ("Current Time:"+DateTime.Now.ToString ()); +Fs. WriteLine ("Exception Information:"+Ex. Message); -Fs. WriteLine ("Exception object:"+Ex. Source); +Fs. WriteLine ("Call stack: \ n"+Ex. Stacktrace.trim ()); AFs. WriteLine ("Trigger Method:"+Ex. TargetSite); at FS. WriteLine (); - FS. Close (); -}
C # Implementation writes exception to log example (Exception log)