ASP. NET Add error log

Source: Internet
Author: User

In the development program, the error log is necessary. Add error logs to use today and log them for easy viewing later

Using the ASP. NET error handling mechanism

Application_Error

Paste the code

1     protected voidApplication_Error (Objectsender, EventArgs e)2         {3C:exception LastErr = Server.GetLastError (). GetBaseException ();//using the GetLastError method of the internal server object, the method returns an exception,4             Try5             {6                 stringPath ="/error/"+ DateTime.Today.ToString ("YYMMDD") +". txt";7                 stringMapPath = HttpContext.Current.Server.MapPath (path);//Specify the physical path corresponding to the virtual path using the MapPath method on the server side8                                                                           //if the path below the file does not exist9                 if(!file.exists (mapPath))Ten                 { OneFile.create (MapPath). Close ();//Create a file below the folder, and close the stream to release A                 } -                 //strength file stream for writing -                 using(StreamWriter writer =file.appendtext (MapPath))//append to file using Apperntext the                 { -Writer. WriteLine ("{0}", DateTime.Now.ToString ()); - writer. WriteLine (Lasterr.Message.ToString ()); -Writer. WriteLine ("-------------------------------"); +Writer. Flush ();//cleans up the buffer and inserts the data into -Writer. Close ();//Close after writing +                 } A             } at             Catch(Exception ex) -             { -                 GotoC; -}

ASP. NET Add error 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.