ASP. NET error logs and asp.net error logs

Source: Internet
Author: User

ASP. NET error logs and asp.net error logs

In this article, we will record errors and exceptions on our website through a simple process. In this way, when a program error occurs, the user is redirected to a separate page, and the error is recorded in a text file on the server. When an error occurs, we will record the log every day.

First, I will write a static method to record the Error information to a text file. Here, I will record the Error information to the Error folder on the server. The Code is as follows:

 

Using System. globalization; /// <summary> /// output error information to the txt file // </summary> /// <param name = "errorMessage"> error details </ param> public static void WriteError (string errorMessage) {try {string path = "~ /Error/"+ DateTime. Today. ToString (" yyMMdd ") +". txt "; if (! File. exists (System. web. httpContext. current. server. mapPath (path) {File. create (System. web. httpContext. current. server. mapPath (path )). close ();} using (StreamWriter w = File. appendText (System. web. httpContext. current. server. mapPath (path) {w. writeLine ("\ r \ nLog Entry:"); w. writeLine ("{0}", DateTime. now. toString (CultureInfo. invariantCulture); w. writeLine (errorMessage); w. writeLine ("________________________________________________________________"); w. flush (); w. close () ;}} catch (Exception ex) {WriteError (ex. message );}}

  

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.