Use Enterprise Library for exception handling Application Block Development

Source: Internet
Author: User
ArticleDirectory
    • 2.1.1-source mode of abnormal application blocks
This document is maintained at: http://wiki.entlib.net.cn/entlibhelp31exceptionhandlingapplicationblock.ashx.

This topic describes how to use an exception handling ApplicationProgramBlock development applications. It first explains how to reference an Application Block in an application and configure it. Then, in key scenarios, it describes
It explains how to use application Blocks Based on specific scenarios, such as logging and propagation exceptions. Finally, in the details of the development task, it explains how to develop an exception handling policy and
How to send exceptions to application blocks.

All application blocks are releasedSource code, You must compile them before the Quick Start and configuration tools are used. Learn how to compile the Enterprise Library SourceCode, See generate Enterprise Library.

2.1-enter configuration information

These procedures explain how to configure the exception handling Application Block and the attributes related to the nodes that appear in the right panel.

Add an exception handling Application Block

    1. Open the configuration file. For more information, see configure application blocks.
    2. Right-clickApplication configuration, PointingNewAnd then clickException Handling Application Block.

Configure exception handling policies

  1. To add an exception handling policy, right-clickExceptionhandlingapplicationblockNode, pointingNewAnd then clickExceptionpolicyThis creates an exception handling Policy node. By default, this exception handling policy is named exception policy.
  2. Rename Policy (optional ). Right-click the Policy node and clickRenameAnd enter the Policy Name.
  3. Add the exception type to the policy, right-click the Policy node, and pointNewAnd then clickException type. In the type selector dialog box, select the exception type. If the type is not listed, clickType SelectorOn the dialog boxLoadassemblyTo find the Assembly that contains the expected type. ClickOK. This createsExceptionNode. The node attributes are displayed on the right panel.
  4. select the posthandlingaction you want. posthandlingaction determines what activity will take place after the exception handling link is completed. By default, posthandlingaction is set to policyrethrow . Its possible values are as follows:
    • none . The Application Block executes all the processing programs for this exception, and then returns false to the application at the handleexception method call point. The application checks this value to continue running.
    • policyrethrow . The Application Block executes all the processing programs for this exception, and then returns true to the application on the handleexception method call point. When the application checks this value, the original exception is thrown again.
    • thrownewexception . The Application Block executes all the processing programs for this exception, and then throws an exception after all the processing programs run.
  5. To add an exception handling program, right-clickExceptionNode, pointingNewAnd then click the type of the exception handler:
    • Custom Handler. This exception handler allows you to configure custom exception handlers. ClickType SelectorOnLoadassemblyTo add a Custom Handler.
    • Logging Handler. This exception handler formats the exception information and then uses the log application block to record the exception information. When you select a log processing program, the log application block is automatically added to the application configuration. For more information, see the log Application Block documentation.
    • Replace Handler. This exception handler replaces an exception with another exception.
    • Wrap Handler. This exception handler wraps one exception into another.

Configure the exception handling program

After adding an exception handler, you can configure it. These procedures explain how to configure custom, logged, replace, and wrap exception handlers.

Configure a customized exception handling program

    1. Click the Custom Handler in the Tree View.
    2. In the right panelAttributesClick the ellipsis in the Properties Section.(...).
    3. InEditablekeyvaluecollectioneditorIn the dialog box, clickAddAdd a new name/value pair.
    4. InEditablekeyvaluecollectioneditorOn the right panel of the dialog box, enter the key name and attribute value.
    5. Add more name/value pairs as appropriate, and then clickOK.
    6. Placed in the right panel of the configuration ConsoleNameAttribute. If needed, modify the name of the exception handling Application Block. The default name isCustom Handler.
    7. ClickTypeEllipsis button in Property Section(...). If the expected type is not included inAssembliesFolder, clickTypeselectorOnLoadassemblyTo find the Assembly that contains the required types.

To configure a log exception handler, click the logging handler node and enter the following values displayed on the right panel:

    • Eventid. This is the number of IDs that will be logged. The default value is100. If needed, you can enter different values.
    • Formattertype. This is the full name of the formatting program used to format exception information before logging. Click the ellipsis(...)And then useTypeselectorSelect the formatting program to use.
    • Logcategory. This is the name of the log category that the event will be assigned. The default name isDefaultcategoryYou can enter different category names.
    • Name. This is the name of the log exception handler. The default name isLogginghandler. You can modify the name if needed.
    • Priority. This is the priority of the log message. Log application blocks can be configured to filter log messages based on their priorities.
    • Severity. This is the severity of the event. The default value isError. To select another severity program, click another entry in the drop-down list. Other options are:Critical, warning, information, verbose, start, stop, suspend, resumeAndTransfer.
    • Title. This is the title of the log entry. The default value isEnterpriselibraryexceptionhandlingYou can modify it as needed.

To configure a replacement handler, click the replace handler node and enter the following values displayed on the right panel:

    • Predictionmessage. This is the message text used to replace the exception. This attribute is optional.
    • Name. This is the name of the replacement handler. The default name isReplacehandler. You can modify it as needed.
    • Replaceexceptiontype. This is the full name that replaces the exception type of the original exception. The default type isSystem. Exception. UseTypeselectorClick the expected type.

To configure a package handler, click the wrap handler node and enter the following value displayed in the right panel:

    • Predictionmessage. This is the message text used to wrap exceptions. This attribute is optional.
    • Name. This is the name of the packaging handler. The default name isWraphandler. You can modify it as needed.
    • Wrapexceptiontype. This is the full name of the exception type that will wrap the original exception. The default value isSystem. Exception. If this is not what you want, useTypeselectorClick the expected type.

2.1.1-source mode of abnormal application blocks

This topic lists the XML elements and attributes used for exception handling application blocks. You can manually edit the XML data, but the Enterprise Library configuration console greatly simplifies this task. If you select to manually edit XML, use the mode information contained in this topic.

The configuration file contains the following section handler declaration.

<Configsections>
<Section name = "exceptionhandling"
Type = "Microsoft. Practices. enterpriselibrary. exceptionhandling. configuration. exceptionhandlingsettings,
Microsoft. Practices. enterpriselibrary. exceptionhandling,
Version = 3.1.0.0,
Culture = neutral,
Publickeytoken = NULL "/>
</Configsections>

The Section handler scenario includes the Configuration Setting section name and the section handler class name for processing the configuration data in that section. The Configuration Setting section name isExceptionhandling. Section handler class name isExceptionhandlingsettings. This class is included inMicrosoft. Practices. enterpriselibrary. exceptionhandling. ConfigurationNamespace.

Predictionhandling Element

ExceptionhandlingElement describes the configuration of abnormal application blocks. This element is required.

Attributes and child elements

The following sections describeExceptionhandlingThe attributes and child elements of the element.

Predictionpolicies sub-element

PredictionpoliciesElement isExceptionhandlingThe child element of the element. It lists the exception policies that an application can call. This element is optional.

ADD child element

AddElement isPredictionpoliciesThe child element of the element.AddElement to add the name of the exception policy. This element is optional and can contain multipleAddElement.

Attribute

Table 1 listsAddThe attribute of the element.

Table 1: Add attributes
Attribute Description
Name The name of the exception policy. The name must be unique in the Section. This attribute is required.

Predictiontypes subelement

PredictiontypesElement isAddThe child element of the element. It lists the types of exceptions that can be handled by the exception policy. This attribute is optional.

ADD child element

AddElement isPredictiontypesChild element of the element,AddElement to add the name of the exception type. This element is optional and can contain multipleAddElement.

Attribute

Table 2 listsAddThe attribute of the element.

Table 2: attributes of the add element
Attribute Description
Name The name of the exception type. The name must be unique in the Section. This attribute is required.
Type Derived fromExceptionClass Name. This attribute is required.
Posthandlingaction Specifies the exception policy to be implemented after the exception handling program is called. The possible value isNone,NotifyrethrowAndThrownewexception. This attribute is required.

Predictionhandlers sub-element

ExceptionhandlersElement isAddThe child element of the element, which specifies an exception handler. This element is required.

ADD child element

AddElement isExceptionhandlersA child element of an element.AddElement to add the name of the exception type. This element is optional and can contain multipleAddElement.

Attribute

Table 3 listsAddThe attribute of the element.

Table 3: Add attributes
Attribute Description
Name The name of the exception handling program. The name must be unique in the Section. This attribute is required.
Type ImplementedIexceptionhandlerThe name of the API class. This attribute is required.
Predictionmessage Messages used by exceptions. This attribute is usedWraphandlerClass andReplacehandlerIt is required.
Wrapexceptiontype Exception type of the original packaging exception. It must have a derived fromExceptionClass Name. This property andWraphandlerAnd is required.
Replaceexceptiontype Replaces the exception type of the original exception. It must have a derived fromExceptionClass Name. This property andReplacehandlerAnd is required.
Logcategory Categories of abnormal message logs. This attribute is a string. It correspondsLoggingexceptionhandlerAnd must be used together.
Eventid Number of abnormal IDs. This attribute is an integer. It correspondsLoggingexceptionhandlerAnd must be used together.
Severity Abnormal serious program. This attribute isTraceeventtypeEnumeration type. It correspondsLoggingexceptionhandlerAnd is required.
Title The title of the log entry. This attribute is a string. It correspondsLoggingexceptionhandlerAnd is required.
Formattertype The formatting program that formats the exception message before the exception is recorded. It must be a derived fromPredictionformatterClass Name. It correspondsLoggingexceptionhandlerAnd is required.
Priority The priority of the log entry. This attribute is an integer. It correspondsLoggingexceptionhandlerAnd is required.


2.2-add application code

Exception Handling application blocks are designed to support most common scenarios for exception handling. When adding application code, refer to the scenario in key scenarios and select the most suitable scenario. Use the code displayed here to complete the scenario, or adapt to it as needed.

Prepare applications to use exception handling application blocks

    1. Add a reference to the exception handling application block assembly. In Visual Studio, right-click the project node in the solution browser, and then clickAdd reference. ClickBrowseTab, selectMicrosoft. Practices. enterpriselibrary. exceptionhandling. dllAssembly, and then clickOK.
    2. Set to the Enterprise Library kernel assembly in the same steps:Microsoft. Practices. enterpriselibrary. Common. dllAndMicrosoft. Practices. objectbuilder. dll. If the application is configured to use the log exception handler, set the reference to the required log application block assembly. For information about the log application block assembly, see the log Application Block documentation.
    3. (Optional) You can add the followingUsingStatement (C #) orImportsStatement (Visual Basic) to the top of the source code file.

C #

Using Microsoft. Practices. enterpriselibrary. exceptionhandling;

Visual Basic

 
Imports Microsoft. Practices. enterpriselibrary. exceptionhandling

Note:For visual basic projects, you can useReferencePage to manage references and import namespaces. To accessReferencePage, select a project node in the solution browserProjectClickAttribute. When the project designer appears, clickReferenceLabel.

Next, add the application code. Generally, you must complete the following steps to use the code for exception handling application blocks:

    1. Capture exceptions.
    2. Handle exception policies.
    3. Throw the original exception again, if it is recommended by the policy.

The example in this section shows how to merge these steps into an application.

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.