Use Exception Management Application Block management exception

xiaoxiao2021-03-06  81

Use Exception Management Application Block management exception

Author: Zheng Zuo

2004-8-31

The handler exception is essential during the usual development process, how to get more exception information and the unified processing of exception information is a thing worth thinking. On the development of the .NET program, Microsoft provides us with a better exception processing module (Exception Management Application Block), which can be used in non-specific .NET applications. Program modules and document descriptions can be downloaded on the Microsoft website, and the MSDN document also has more detailed descriptions.

Using this module has been many times, I read the code inside, I feel very good, some design is worth reference and brought. So I wrote some explanations and share.

First, the layout of the component

The entire component layout is as follows:

(1)

Second, the main code analysis

I think the most critical is two classes: ExceptionManagerSectionHandler and ExceptionManager class.

(1) ExceptionManagerSectionHandler class

Its function is to read the information of the custom configuration section in the program profile, see the System.Configuration.iconfigurationSecationHandler interface from the code.

Public Class ExceptionManagerSectionHandler: iConfigurationSectionHandler

When implementing the iConfigurationSectionHandler.create method, read configuration information in the configuration file to the ExceptionManagementSettings class instance.

Let's first look at the setting information of a standard profile before reading the processing inside the method:

CONFIGSECTIONS>

EXCETIONMANAGEMENT>

CONFIGURATION>

Name of the custom section to "exceptionManagement", named Type attributes in the class ExceptionManagerSectionHandler, so that by ExceptionManagementSettings config = (ExceptionManagementSettings) ConfigurationSettings.GetConfig ( "exceptionManagement"); to obtain complete encapsulation information, very easy to use. The analysis of the ExceptionManagement element is done in the create () method. public object Create (object parent, object configContext, XmlNode section) {// ...... ExceptionManagementSettings settings = new ExceptionManagementSettings (); // ...... currentAttribute = nodeAttributes.RemoveNamedItem (PUBLISHER_TYPE); if (! currentAttribute = null) publisherSettings.TypeName = CurrentAttribute.Value; // ...} Remove XMLNode using the XMLNameDNodeMap.removenamedItem (String) method, which does not need to handle excessive things. A better design is to increase the custom attributes and values ​​for the publication of the Publisher element. As the filename = "c: /quickstartsamplexceptionlog.xml in the above configuration, this is flexible and can use specific configuration information when handling specific exceptions. The following additional information is filled into the publisherSettings AddOtherAttributes code: for (int i = 0; i

(2) ExceptionManager class This class uses a private constructor to prevent being instantiated. private ExceptionManager () {} class ExceptionManager Before discussing briefly look DefaultPublisher the class that implements the interface IExceptionPublisher, public sealed class DefaultPublisher: IExceptionPublisher function abnormality information is published to the system log to be accomplished by the method IExceptionPublisher.Publish For specific work, we will discuss it again in the next section. Going back and then see the main way of ExceptionManager, public static void public publish (Exception Exception, NameValueCollection) In this method body, it will first determine whether there is a module configuration information in the program configuration file. If there is no indirect call DEFAULTPUBLISHER.PUBLISH method abnormality information to publish; if (ConfigurationSettings.GetConfig (EXCEPTIONMANAGEMENT_CONFIG_SECTION) == null) {PublishToDefaultPublisher (exception, additionalInfo);} EXCEPTIONMANAGEMENT_CONFIG_SECTION represents "exceptionManagement" string; if it has access to information and encapsulated ExceptionManagementSettings instance, ExceptionManagementSettings config = (ExceptionManagementSettings ) ConfigurationSettings.GetConfig (EXCEPTIONMANAGEMENT_CONFIG_SECTION); config in the analysis of call information to select the next few private static method for publishing exception information 1) PublishToDefaultPublisher method published by the default mode, which is calling DefaultPublisher.Publish method, the entire method is as follows , private static void PublishToDefaultPublisher (Exception exception, NameValueCollection additionalInfo) {DefaultPublisher publisher = new DefaultPublisher (); Publisher.Publish (exception, additionalInfo, null);} 2) PublishInternalException method is used to release the internal processing module The errors generated during the process, we can see it from its methodology to write the information "Application" log. It is worth noting that this method adds an Internal modifier.

internal static void PublishInternalException (Exception exception, NameValueCollection additionalInfo) {DefaultPublisher Publisher = new DefaultPublisher ( "Application", resourceManager.GetString ( "RES_EXCEPTIONMANAGER_INTERNAL_EXCEPTIONS")); Publisher.Publish (exception, additionalInfo, null);} 3) PublishToCustomPublisher method used Publish custom exception information, decide to create an instance of the class that implements the IExceptionXMLPublisher interface based on the Publisher.ExceptionFormat enumeration value. if (publisher.ExceptionFormat == PublisherFormat.Xml) {IExceptionXmlPublisher XMLPublisher = (IExceptionXmlPublisher) Activate (publisher.AssemblyName, publisher.TypeName); XMLPublisher.Publish (SerializeToXml (exception, additionalInfo), publisher.OtherAttributes);} else {IExceptionPublisher Publisher = (IExceptionPublisher) Activate (publisher.AssemblyName, publisher.TypeName); Publisher.Publish (exception, additionalInfo, publisher.OtherAttributes);} method of generating instance follows: private static object Activate (string assembly, string typeName) {return AppDomain. CurrentDomain.CreateInstanceAndUnwrap (assembly, typeName);} another method is to sequence the abnormality information into XmlDocument object, defined as follows: public static XmlDocument SerializeToXml (exception exception, NameValueCollection additionalInfo) {// ...} method body is to add the information to An XMLDocument document is generated in the XML node or attribute. Third, the custom interface will be used by the user to implement custom exception handling, in Figure (1) we can see that the program brings us two interfaces, IExceptionXMLPublisher, and IExceptionPublisher, and defines the publication method.

As follows: public interface IExceptionPublisher {void Publish (Exception exception, NameValueCollection additionalInfo, NameValueCollection configSettings);} public interface IExceptionXmlPublisher {void Publish (XmlDocument exceptionInfo, NameValueCollection configSettings);} Nothing, just to illustrate the above parameters used in the method: Exception represents an exception class, which is an exception generated in the program. The exception class defined in the program is generated from the BaseApplicationException class, which can contain the following information: 1> Environment.machinename 2> thread.currentprincipal.Identity.name 3> Windowsidentity. GetCurrent (). Name 4> AppDomain.CurrentDomain.FriendlyName additionalInfo, abnormality refers to the additional information, it comprises the following, 1> Environment.MachineName 2> DateTime.Now 3> Assembly.GetExecutingAssembly (). FullName 4> AppDomain.CurrentDomain.FriendlyName 5 > Thread.currentprincipal.Identity.Name 6> Windowsidentity.getCurrent (). Name Exceptioninfo object is obtained by using ExceptionManager.SerializeToxML method to XmLDocument. ConfigSettings, which contains some additional information added by the configuration file, such as FileName = "C: /quickStartSamplexceptionLog.xml" above, can be multiple.

转载请注明原文地址:https://www.9cbs.com/read-121389.html

New Post(0)