Asp. NET logging of error logs

Source: Internet
Author: User

In this article, we will record errors and exceptions in our website through a simple process. We do this, each time we encounter a program error, the user navigates to a separate page, and the error will be logged to a text file on the server, and whenever an error occurs, we will log each day in the form of logs.

First, I write a static method to log the error message to a text file, here is the error message logged to the server in the error folder, the code is as follows:

Using System.Globalization; <summary>///For outputting error information to TXT file///</summary>//<param name= "errormessage" > Error details </para  m> 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.HttpC Ontext. 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); }    }

  

Asp. NET logging of error logs

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.