Accumulate [C #] --- the error log is recorded in the txt text .,

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.