ASP. NET Error Handling Module

Source: Internet
Author: User

Exceptions can be captured through try catch, but it is inevitable that several of them forget catch. This will cause errors such as 500 on the page. The error information can still be seen on the local machine, but if it is already online, the thrown exception will not be visible to the user. If you want to handle it, you can go to the windows Log and add a module to capture the exception information and send it to relevant persons.
 
Create a project, add a class ErrorReporterModule, inherit from IHttpModule, and implement two methods. Void Init (HttpApplication context) and void Dispose ()
 
Add the following code under Init:
1: context. Error + = (object sender, EventArgs e) =>
2 :{
3: HttpContext ctx = HttpContext. Current;
4: HttpRequest request = ctx. Request;
5: Exception exception = ctx. Server. GetLastError ();
6:
7: HttpErrorMessage message = new HttpErrorMessage ()
8 :{
9: Message = exception. Message,
10: StackTrace = exception. StackTrace,
11: Source = exception. Source,
12: HelpLink = exception. HelpLink,
13: RequestForm = request. Form,
14: RequestParam = request. Params,
15: RequestUrl = request. Url. ToString (),
16: QueryString = request. QueryString,
17: Cookies = request. Cookies
18 :};
19: container. Resolve <ErrorRecorder> (). Record (message );
20: ctx. Server. ClearError ();
21 :};
The rest is to handle these exceptions. The main error information is in the Exception, and the error environment is in the request.
Handling thrown exceptions can be saved to the database by means of text record, but these operations are passive. developers can view the information from time to time. Of course, we can also configure an email service to send these error messages to developers.
We can also configure related processing personnel so that they can be sent to corresponding development and testing sites based on different sites.
NEXT: Configure web. config
Add the following in web. confg:
<HttpModules>
<Add name = "ErrorReporterModule" type = "Symbol. Web. Mvc. Modules. ErrorReporterModule"/>
</HttpModules>
You can,
The classic mode of II7 is the same as that of II6, which is compatible with iis6.
However, in the integration mode in II7, we also need to add one in system. webServer.
 
Finally, record idea:
1. Configure the corresponding module developer. If a module error occurs, the developer will follow up.
2. Set a scheduled task, such as exporting an exception report within one week.
3. Configure multiple sending modes, such as text, database, and email...
4. identify different exceptions. For example, a user may make a mistake in the URL, which does not need to be sent.

From Mylhei's. NET HOME

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.