C # Implementation writes exception to log example (Exception log)

Source: Internet
Author: User

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)

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.