zoukankan      html  css  js  c++  java
  • Enterprise Library 2.0 Exception Handing Applcation Block

               Enterprise Library 2.0 中的 Exception Handing Applcation Block 为我们开发人员以及设计人员提供了一个一致的方法去处理我们整个企业级应用程序的每个层次上的异常,表现在以下几个方面:

    1、它为应用程序的架构的每个层提供异常处理方法,并不仅仅局限于服务层接口;
    2、提供通用的异常处理方法,比如记录异常信息,使用我们自定义的异常信息来替换系统的异常信息等等;
    3、我们可以自己定义异常处理的规则,并且可以不通过修改代码来修改异常处理的规则;
    4、它支持我们开发者自己创建自己的异常处理方法;
    5、通过同样的方法来调用异常处理方法,这使得异常处理可以用于应用程序的任何地方。

    我们在进行异常处理时,一般采用一下策略:
    1、包装
    2、替换
    3、日志

    那么,Exception Handing Application Block 究竟能给我们得开发带来哪些好处呢?先看下面的代码:

    DataSet customersDataSet;

    try
    {
      customersDataSet 
    = RunQuery("GetAllCustomers");
    }

    catch(Exception ex)
    {
      
    string formattedInfo = FormatException(ex);
      Logging.Log(formattedInfo);
      
    throw new DataAccessException("Database access failure for query GetAllCustomers",e);
    }

    上面的代码估计我们最熟悉不过了,不错,在我们的应用程序中这样的代码估计会贯穿我们的程序中的每一个角落,但是当我们处理异常的方法发生改变了的时候,我们就要对所有的代码进行修改,这是一件很郁闷的事情,Exception Handing Application Block的出现就为我们解决了这样的麻烦,使用Exception Handing Application Block后,我们只需要修改配置文件就可以改变应用程序中的所有异常处理方法。如下:

    DataSet customersDataSet;

    try
    {
      customersDataSet 
    = RunQuery("GetAllCustomers");
    }

    catch(Exception ex)
    {
      
    bool rethrow = ExceptionPolicy.HandleException(ex, "Data Access Policy");
      
    if (rethrow)
        
    throw;
    }

    其中 Data Access Policy 就是一个异常处理的策略,具体的配置和用法我们将在后面说到,当我们的异常处理方式需要改变的话,我们只需要修改配置文件即可,下面我们先来看一下Exception Handing Application Block的配置方法(以日志策略为例):

    1、打开配置工具,新建一个Exception Handing Application Block;



    2、选中我们刚刚新建的Exception Handing Application Block,右键单击,在弹出的菜单中选择New-->Exception Policy,新建一个异常处理的策略,命名为UI Policy;




    3、在新建的异常处理策略下新建一个Exception Type,选择异常类型为System.Exception;




    4、设置Exception的PostHandingAction为None,表示异常处理结束之后不执行任何操作,可选项还有ThrowNewException和NotifyRethrow;



    5、接下来我们需要在Exception下新建一个Logging Handler


    6、设置其FormatterType为Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter,然后设置LogCategory为DataBase,这里的DataBase是我在Logging Application Block中建的,具体配置方法可以参考下面的文章:
    Enterprise Library 2.0 -- Logging Application Block (上)
    Enterprise Library 2.0 -- Logging Application Block (下)
    Enterprise Library 2.0 -- Logging Application Block (补充)

    我们还可以设置其EventID,Name,Title等辅助信息:



    7、上面的配置结束之后,我们保存我们的Project,就完成了一个日志策略的异常处理的配置,最终的配置文件如下:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      
    <configSections>
        
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, 

    Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
     />
      
    </configSections>
      
    <exceptionHandling>
        
    <exceptionPolicies>
          
    <add name="UI Policy">
            
    <exceptionTypes>
              
    <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                postHandlingAction
    ="None" name="Exception">
                
    <exceptionHandlers>
                  
    <add logCategory="DataBase" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
                    formatterType
    ="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, 

    Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"

                    priority
    ="0" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, 

    Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"

                    name
    ="Logging Handler" />
                
    </exceptionHandlers>
              
    </add>
            
    </exceptionTypes>
          
    </add>
        
    </exceptionPolicies>
      
    </exceptionHandling>
    </configuration>

    上面的配置做好之后,我们在程序可以根据下面的方法进行日志策略的异常处理:

    try
    {
      
    // Run code.
    }

    catch(Exception ex)
    {
      
    bool rethrow = ExceptionPolicy.HandleException(ex, "Logging Policy");
      
    if (rethrow)
        
    throw;
    }

    关于替换策略和封装策略的使用方法以及配置方法和日志策略基本上相同,这里就不在多说了。

    异常处理这一块我们先说到这里,更深入的部分我会在以后的文章中继续和大家讨论,欢迎大家积极的讨论有关这方面的问题,只有相互交流才能达到共同进步!

    Email:pwei013@163.com


  • 相关阅读:
    windows系统切换jdk,修改java_home无效情况
    Cannot instantiate interface org.springframework.context.ApplicationListener
    MySQL分组查询获取每个学生前n条分数记录(分组查询前n条记录)
    ASP.NET Web API 使用Swagger生成在线帮助测试文档,支持多个GET
    EF TO MYSQL 无法查询中文的解决方法
    HttpWebRequest post请求获取webservice void数据信息
    This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms. 此实现不是 Windows 平台 FIPS 验证的加密算法的一部分 解决方案
    MySQL 5.7.13解压版安装记录 mysql无法启动教程
    C# udpclient 发送数据断网后自动连接的方法
    汽车XX网站秒杀抢购代码
  • 原文地址:https://www.cnblogs.com/pw/p/435179.html
Copyright © 2011-2022 走看看