Exception management application block released by Microsoft

Source: Internet
Author: User

In order to better manage how to publish an exception to the application to the customer, if the administrator is notified by email, or the exception information is written into the log, it is directly displayed on the page through friendly information. for this reason, Microsoft has encapsulated an exception management module EAB.

Generally, we write the Exception Handling Code as follows:

Try
... Code that might cause an exception...
Catch ex as exception
... Connect to database...
... Insert a row into an exceptionlog table, adding details
About the exception...
End try

However, this is very troublesome to change the abnormal reporting behavior. If we use eantibody, we can write it like this:

Try
... Code that might cause an exception...
Catch ex as exception
Predictionmanager. Publish (Ex)
End try

In EAB, there is a class called exceptionmanager. Its publish method receives an exception instance.
The handling of report exceptions is determined by a class that inherits the iexceptionpublisher interface. Of course, this interface is defined in EAB. then we will go to the famous web. register the implemented interface class and its assembly in the config file.

For example, dbexceptionpublisher class implements the iexceptionpublisher interface:

Imports Microsoft. applicationblocks. exceptionmanagement
Imports system. Web
Imports system. Data. sqlclient
Imports Microsoft. applicationblocks. Data

Public class dbexceptionpublisher
Implements iexceptionpublisher

Public sub publish (byval exception as system. exception ,_
Byval additionalinfo as namevaluecollection ,_
Byval configsettings as namevaluecollection )_
Implements iexceptionpublisher. Publish
Try
Sqlhelper. executenonquery (globalconnectionstring,
Commandtype. storedprocedure, "sp_addexception ",_
New sqlparameter ("@ message", exception. Message ))
Catch ex as exception
Httpcontext. Current. response. Write ("Fatal error: <br> "&_
Ex. stacktrace & "<br>" & Ex. Message)

Httpcontext. Current. response. Flush ()
Httpcontext. Current. response. End ()
End try
End sub
End Class

Then register in Web. config as follows:

<Configuration>
...
<Exceptionmanagement mode = "on">
<Publisher Assembly = "myprojectexceptionblock"
Type = "myprojectexceptionblock. dbexceptionpublisher"/>
</Exceptionmanagement>
...
</Configuration>

We write exception handling code like this in the program:

Imports Microsoft. applicationblocks. exceptionmanagement

Public class Logon
Inherits system. Web. UI. Page

Private sub page_load (byval sender as object, byval e as eventargs)
Try
... Do something that might cause an exception...
Catch ex as exception
Predictionmanager. Publish (Ex)
End try
End sub

...
End Class

In this way, the exception reporting behavior in the System displays the exception information directly on the page. If you want to change it to an email notification to the Administrator, you can also write an iexceptionpublisher class inheritance interface on the web. update Registration in config.

Exception management application block (emonoclonal) is as follows:

Http://download.microsoft.com/download/VisualStudioNET/emabref/RTM/NT5/EN-US/ExceptionManagementApplicationBlock.msi

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.