C # Logging error logs (introduction, method Code)
Introduction: The Program in error logging error log, can be conducive to maintenance, you can also record some of the procedures inside the operation and so on, it is very important, but also
Method 1:
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 path logaddress is empty, the new Yyyy-mm-dd-log.log file is created by default in the Debug directory9 if(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 # Logging error logs