Asp. NET global error handling and exception logging and IIS configuration custom error pages

Source: Internet
Author: User
Tags log4net

application scenarios and purpose of use

Many times, when we visit the page, due to program anomalies, system crashes will lead to the Yellow pages. In the usual case, the Yellow pages are great for us, because it helps us to know the source of the problem and even what line of code is wrong. But this is very scary for the user, because the user does not know what happened, and can not understand the contents of the Yellow Pages. Even if we meet some unfriendly people, they will make a fuss about the content and pose a threat to our website.

So how do we in the program exception, the system crashes, will not appear yellow pages, and can also give some more friendly hints? Even when we need to, we can collect these abnormal information and analyze it, can quickly locate the source of the error, solve the problem? Three solutions are given below.

user-defined error using customerrors

Add under the <system.web> node of the Web. config

1 <customErrorsMode= "RemoteOnly"defaultredirect= "/error/error.htm">    2     <ErrorStatusCode= "404"redirect= "/error/404.html" />3     <ErrorStatusCode= "403"redirect= "/error/403.html" />4     <ErrorStatusCode= "$"redirect= "/error/500.html" />5 </customErrors>

mode Specifies that custom errors are enabled, disabled, or displayed only for remote clients. Mode=remoteonly is the default value that specifies that custom errors are displayed only to the remote client side and that an ASP. NET error is displayed to the local host. When Mode=on, if no defaultredirect is specified, the user sees a generic error. When Mode=off, a detailed error is displayed.

defaultredirect Specifies the default URL that the browser points to when an error occurs. If defaultredirect is not specified, a generic error is displayed.

StatusCode Specifies the HTTP status code that causes the redirect to the error page.

Redirect error page that provides error information to the client.

use httperrors, overwrite customerrors

Highly recommended, why? By default, customerrors jumps to the custom error page with a 302 redirect. When we ask for an error page, the page will start with a 302 redirect and then return to 200OK. For the user, this is completely no problem, because it has done a friendly user experience. But for crawlers, it thinks it's a wrong page, like 404, and it's going to be typed. Moreover, customerrors each redirect with a small tail aspxerrorpath, such as Http://yoursite.com/error/404.html?aspxerrorpath=/exceptionpageurl. There are also times when customerrors belongs to a custom configuration under a managed environment, CustomErrors does not work when the request has not yet reached the ASP, so using HttpErrors also includes the ability to handle requests that can also catch exceptions without reaching the ASP.

Add under the <configuration>/<system.webServer> node of the Web. config

1 <httperrorsErrormode= "Custom"Existingresponse= "Replace"Defaultresponsemode= "Executeurl">2      <RemoveStatusCode= "403"Substatuscode= "-1"/>3      <RemoveStatusCode= "404"Substatuscode= "-1"/>4      <RemoveStatusCode= "$"Substatuscode= "-1"/>5      <ErrorStatusCode= "403"Path= "/403.html"Prefixlanguagefilepath=""Responsemode= "Executeurl"/>6      <ErrorStatusCode= "404"Path= "/404.html"Prefixlanguagefilepath=""Responsemode= "Executeurl"/>7      <ErrorStatusCode= "$"Path= "/500.html"Prefixlanguagefilepath=""Responsemode= "Executeurl"/>8  </httperrors>

See Http://www.iis.net/configreference/system.webserver/httperrors for details.

The error page is processed uniformly in the Application_Error method in Global.asax and logs are logged

The first two can be custom error pages, but only cannot customize the logging, and this way can meet both requirements.

1   protected voidApplication_Error (Object sender, EventArgs e)2   {3Exception LastError =Server.GetLastError ();4   5       if(LastError! =NULL)6       {7           if(LastError ishttpexception)8           {9Server.ClearError ();//Clear Error statusTenServer.Transfer ($"/error/{response.statuscode}.html");//jump to a page that specifies a friendly experience One                   //logs can be logged in two ways at the same time A                   //The first use of log4net -                   //second use of the database -          } the      } -}

Log4net record abnormal log, the garden has a lot of articles in detail explained, here will not repeat. The database records the Exception log, to explain the table design, because it is possible to consider that this is not the only place to record the Exception log, as well as in application_beginrequest,application_endrequest and so on. Therefore, the table design field should contain the current context, the reported exception source, exception information and other fields. This will be very important for us to quickly locate errors and troubleshoot anomalies in the future.

It is also important to note that Server.Transfer does not change the URL, and if you need to change the URL, you can use Response.Redirect to jump.

Summarize

customerrors because of the disadvantage of SEO, so now is not recommended to use.

The Httperrros does not cause a 302 redirect, and it captures exception requests that do not reach the ASP, but cannot customize the Exception log.

Application_Error can also customize the error page, or you can customize the log. However, it is important to note that Application_Error is also in a managed environment and cannot capture exception requests that do not reach the ASP.

Therefore, according to three kinds of solutions, the first push httperrors and Application_Error.

Asp. NET global error handling and exception logging and IIS configuration custom error pages

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.