Logging errors using ASP.net 2.0

Source: Internet
Author: User
Tags config datetime httpcontext log

In this article, we will record errors and exceptions in our website through a simple process. We do this: whenever a program error is encountered, the consumer is navigated to a separate page. Also, errors are logged to a text file on the server. Whenever an error occurs, We will log in the form of a daily record. Say so much, let's look at some code.

Step One: First create an error folder to hold the error log file. right mouse button site > create a new folder. Name the folder "Error". If you do not have a Web.config file in your site, add one. Right-click Site > Add new Project > Web.config.

Step two: Now we're going to create an error-handling code. We just need to right-click Site > Add New Project > select Class. Rename the class to "ErrHandler.cs" and click the Add button. When you do this, a dialog box pops up, and if you want to keep the class file in "App_Code", we choose to accept it.

Step three: Now we add some functionality to the Errhandler.class. This class is used to accept error messages and to save error messages in a text file. Create one such text file every day. If the same file name is already present, the error message is appended to the file. Creates a new file and writes the error message to the file.

The code appears as follows:

Handles error by accepting the "error message"///displays the page on which the error occured public static void Writeerror (String errormessage) {try {string path = "~/error/" + DateTime.Today.ToString ("dd-mm-yy") + "
      . txt "; if (! File.exists (System.Web.HttpContext.Current.Server.MapPath (path))) {File.create (System.Web.HttpContext.Curre Nt. Server.MapPath (Path)).
      Close (); Using (StreamWriter w = file.appendtext (System.Web.HttpContext.Current.Server.MapPath (path)) {W.WR
        Iteline ("\r\nlog Entry:");
        W.writeline ("{0}", DateTime.Now.ToString (CultureInfo.InvariantCulture)); string err = "Error in:" + System.Web.HttpContext.Current.Request.Url.ToString () +.
        Error message: "+ errormessage;
        W.writeline (ERR);
        W.writeline ("__________________________");
        W.flush ();
      W.close (); } catch (Exception ex) {Writeerror (ex).
    message); }   }

This is our ErrHandler class. Then let's look at how to use this class and handle errors in the page level (application level).

Related Article

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.