ELMAH (ASP. NET error logging and notification) series-Concepts

Source: Internet
Author: User

I have recently studied several ASP. in the open-source project of NET, ELMAH is used to record the error log of the entire application. So I took a little research and shared it with you here, if you have used it, please advise.

ELMAH will be introduced in three series:

  • Concepts
  • Basic Applications
  • Advanced Applications

Concepts

ELMAH (The Error Logging Modules And Handlers) is called "Error Log Module And processing", which providesCentralized recording and notification error logs. It is dedicated to ASP. NETHot swappingError logging tool. ASP is not required. NET program, you can configure the web. config (or machine. config) to implement the entire application or even all ASP in IIS.. NET application error logging.It supports multiple log storage methods (various database, XML, and memory storage). In addition to providing an interface for querying log details, you can also send an error message to related personnel through email, RSS subscription, or Twitter Publishing.

For an ASP. NET program, if you want to use ELMAH to add the error log record function for the program, you only need to copy the ELMAH dll file to the bin directory of the program, and then modify the ASP. NET Program web. config file. In the future, if you want to view the system error information, enter the page address specified in web. config to view the log information. For more information, see the next article: application. This article briefly introduces its basic concepts and operating mechanism.

1. ELMAH is a hot swappable solution. That is to say, it can be dynamically added to ASP. and does not need to re-compile and deploy the project. configuration in config, and copy some assembly to the bin directory, without changing any other content in the project (If your project code may cause ELMAH to be unable to record some exception information, you need to modify the relevant Code. For details, see the following 4th ).

2. ELMAH only records unprocessed exceptions in the background. When an exception occurs, it does not change the user experience, that is, according to the software settings, the user may still see the yellow-colored error page or be redirected to an error prompt page. In fact, from the perspective of user experience, a friendly error page should be created.Reference Article 2.

3. ELMAH uses HTTP modules and HTTP handlers to record and display uncaptured exceptions in the software. (if an exception is caught through catch, it cannot be recorded unless it is captured and throw again. That is, in the exception chain, the final exception must be thrown to the ASP. NET runtime before it can be captured ). In addition, if the unhandled exception is ASP. NET Web service, because this exception is not sent to HTTP modules, ELMAH cannot be recorded. this exception is caused by ASP. NET intercepts and returns a SOAP falut (Transmission Error and status information in the SOAP message). Therefore, if you want to record this exception, you can create a SOAP extension to listen to the SOAP faults.

Here we will briefly introduce HTTP modules and HTTP handlers:

When a request arrives at IIS, if the requested resource is configured for processing by ASP. net isapi, IIS will distribute the requestaspnet_isapi.dllAnd the dll handed over the request to the ASP. NET engine for processing. In this engine, there are multiple HTTP modules (which can be in the web. configuration in config), each request must pass these HTTP modules in sequence, so that each HTTP modules can process these requests according to their own needs. After all the HTTP modules, one (only one) HTTP handler will be used to process this request. the HTTP handler configured in config is determined based on the path name or suffix name of the requested resource. For example, you can specify the request error. the resource file axd is processed by the HTTP handler named ErrorLog .). After processing, the HTTP handler returns the result to the user. The process is the opposite. Through each HTTP modules, the result is finally sent to the user end, as shown below:

 

Next let's take a look at how ELMAH configures httpModules and handlers based on the above mechanism.
// ELAMH adds an HTTP module so that each request and response can pass through it. When an error is returned when a user requests a resource, // when the error message is returned, ELMAH obtains the error information in the response through the HTTP module ErrorLog and records it. <HttpModules> <add name = "ErrorLog" type = "Elmah. errorLogModule, Elmah "/> 

4. ELMAH exception capture is an Error event based on the HttpApplication object. If some processing in the software project causes the HttpApplication event to fail to be triggered (for example, if the Application_Error has not been executed before an exception occurs, the Server is executed. clearError () method, which prevents the triggering of the Error event. For example, if an exception is caught by try-catch and throw is not thrown again, then the exception will not trigger the Error event), then ELMAH will be powerless. Therefore, we need to ensure this.

5. since you can configure a target web. the config File and Copy the DLL file to the bin directory of the project to record errors of a specific project. You can also configure the machine. config and put the dll file in GAC to implement all ASP.. NET project implementation error logging.

6. There is also a common solution for. NET Exception Handling: Exception Management Application Block (emonoclonal ). It is a module of Enterprise Library. Here we do not mainly discuss who is better than ELMAH, but which is more suitable for a specific application. The following is a simple comparison:

(1): The ELMAH mechanism determines that it is only used for ASP.. NET WinForm and ASP. NET, but it is more suitable for WinForm, because it records logs in the event viewer of the operating system by default, the Event Viewer is suitable for recording streamlined log information, and this for ASP. NET. If it is running on a shared host and other spaces, it also needs to be allocated to ASP.. NET read/write Event Viewer permission, which is obviously sometimes not feasible. Of course, you can also customize other log recording methods, but all this needs to be implemented by developers.

(2): emonoclonal is not a hot swappable solution and needs to be stored in Global. asax'sApplication_Error(Or other locations. ELMAH is hot-swappable, and requires no code or software re-compilation.

7. In fact, there are still a lot of Logging tools under. NET, such as the well-known Log4net and the Logging Application Block in the Enterprise Library. It can be said that these tools are too heavyweight compared with ELMAH. They can record various log information, such as monitoring variable changes in the Code, periodically record the data to files for other applications to perform statistical analysis; track the code runtime trajectory as the basis for future audits; act as a debugger in the integrated development environment, print the code debugging information to the file or console. Therefore, if you only record ASP. NET error logs, ELMAH should be the first choice, while other features, ELMAH will not be able to do so. That is to say, there is no such thing as good or bad, but which one is more suitable for it.

This is the basic concept. For more information about the operating mechanism, see the followingReference Article 1.

Note: Thanks to Zhou Gong for pointing out a mistake. I don't know if you will be able to solve it :)

References:
1. Using HTTP Modules and Handlers to Create Pluggable ASP. NET Components

2. customErrors Element (ASP. NET Settings Schema)

3. HTTP module and processing program in ASP. NET

4. ELMAH Homepage

5. Detailed configuration of ELMAH web. config (v1.1)

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.