The record error log of--io flow in the first knowledge IO stream

Source: Internet
Author: User

A variety of exceptions are often encountered in Web projects, and in addition to debugging, we can use text to record exceptions. This also allows you to quickly find out where the exception is.

  

Write a quiz below. When the page loads, an exception is given in the Page_Load () method, and after the exception is caught, it is called to write a method that writes the exception to the text file.

Specific implementation:

1 protected voidPage_Load (Objectsender, EventArgs e)2         {3 4             stringTest_str ="I See you! ";5 6             Try7             {8 9Int32 Test_num =Convert.ToInt32 (TEST_STR);Ten  One             } A             Catch(Exception ex) -             { -  the GetErrorMessage (ex. ToString ()); -  -             } -         } +  -  +  A  at  -          Public Static voidGetErrorMessage (stringerrormessage) -         { -             Try -             { -                 /*relative path (file vs. project)*/ in                 stringoutfile ="/error/"+"error-"+ DateTime.Today.ToString ("YYYY-MM-DD") +". txt"; -  to                 /*gets the root directory of the project and the full path to the outfile group composition file*/ +                 stringPath = AppDomain.CurrentDomain.BaseDirectory +outfile; -  the                 /*determines whether the file exists and creates if it does not exist*/ *                 if(!file.exists (path)) $                 {Panax Notoginseng file.create (path). Close (); -                 } the                 using(StreamWriter writer =file.appendtext (path)) +                 { A                     /*write custom data and get exception information*/ theWriter. WriteLine ("errormessage:"); +Writer. WriteLine ("Time : {0}", DateTime.Now.ToString ("YYYY/MM/DD Hh:mm:ss")); -Writer. WriteLine ("Url: {0}", System.Web.HttpContext.Current.Request.Url.ToString ()); $Writer. WriteLine ("detial: {0}", errormessage); $Writer. WriteLine ("————————————————————————————————————————————————————————————————————"); -                 } -             } the             Catch(Exception ex) -             {Wuyi GetErrorMessage (ex. ToString ()); the             } -}

This allows us to find our log file in the error directory of the project, and can quickly find the exception through the log file. Here is the log of the implementation:

  

In a specific project, you can write a tool class to implement the method, the code becomes concise, but also can make the whole system's architecture clearer.

The record error log of--io flow in the first knowledge IO stream

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.