Accumulate [C #] --- the error log is recorded in the txt text .,
Effect:
Description: catch the error information in the system to the log.
Code:
[Backend Code]
1 using System; 2 using System. collections. generic; 3 using System. configuration; 4 using System. IO; 5 using System. linq; 6 using System. web; 7 8 namespace GetLog 9 {10 public class WriteLog11 {12 private static StreamWriter streamWriter; // Write File 13 14 public static void WriteError (string message) 15 {16 try17 {18 // DateTime dt = new DateTime (); 19 string directPath = ConfigurationManager. appSettings [" LogFilePath "]. ToString (). Trim (); // obtain the folder path 20 if (! Directory. exists (directPath) // determines whether a folder Exists. If not, create 21 {22 Directory. createDirectory (directPath); 23} 24 directPath + = string. format (@ "\ {0 }. log ", DateTime. now. toString ("yyyy-MM-dd"); 25 if (streamWriter = null) 26 {27 streamWriter =! File. Exists (directPath )? File. CreateText (directPath): File. AppendText (directPath); // you can create a File if it does not exist. If it exists, add it. 28} 29 streamWriter. writeLine ("************************************* **********************************"); 30 streamWriter. writeLine (DateTime. now. toString ("HH: mm: ss"); 31 streamWriter. writeLine ("output information: error message"); 32 if (message! = Null) 33 {34 streamWriter. WriteLine ("exception message: \ r \ n" + message); 35} 36} 37 finally38 {39 if (streamWriter! = Null) 40 {41 streamWriter. Flush (); 42 streamWriter. Dispose (); 43 streamWriter = null; 44} 45} 46} 47} 48}
[Call ]:
Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; namespace GetLog {public partial class testLog: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {} protected void btn_Click (object sender, EventArgs e) {try {int intStr = Convert. toInt32 (tb. text); tb2.Text = "converted successfully:" + intStr. toString ();} catch (Exception ex) {WriteLog. writeError (ex. toString (); throw ex ;}}}}
Download Demo:
Http://files.cnblogs.com/files/xinchun/GetLog.zip