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