通用异常处理框架(3)

发表于:2014-04-28来源:博客园作者:風語者·疾風点击数: 标签:
在这个默认的异常处理类的ProcessExeception方法中首先调用该类中处理日志记录的方法,再根据配置中的ExReturnMode决定返回处理的结果方式,对异常进行处理

  在这个默认的异常处理类的ProcessExeception方法中首先调用该类中处理日志记录的方法,再根据配置中的ExReturnMode决定返回处理的结果方式,对异常进行处理。特别是当设置为返回方式为Exception,即抛出异常对象时是先判断该异常是否是最初发生的异常,还是已经处理包装过的异常,避免重复处理异常(不管异常是来自本层或者其他层)。

  ExManagement.LogHandler包

  该包只有一个默认的DefaultLogHandler类,实现了ILogHandler接口,它负责把异常信息记录到数据库中。

  我的项目中使用的各层配置文件(示例)

  BusinessLogicLayer

<ExManager ErrorCodeSource="DB" ConnectionString="Data Source=PDMDEV151.ITDEV.ZTE.COM.CN;user id=PDM;password=" DataTable="ErrorInfo">

<ExHandler Name="BusinessLogicLayer" Type="ExManagement.Handler.DefaultExHandler, ExManagement.Handler" ReturnMode="Exception" AlertType="None"/>

<LogHandler Type="ExManagement.LogHandler.DefaultLogHandler, ExManagement.LogHandler" ConnectionString="Data Source=PDMDEV151.ITDEV.ZTE.COM.CN;user id=PDM;password=" DataTable ="ExceptionLog"/>

ExHandler>

ExManager>

  配置意义为:根据ErrorId到数据库ErrorInfo库中获取ErrorString,定义了一个ExHandler,名为BusinessLogicLayer的框架默认的异常处理类,异常返回方式为抛出异常对象,因为不是UI层,所以AlerType为None,该异常处理类用一个默认的LogHandler把异常日志记录到数据库ExceptionLog。

  BusinessFacadeLayer

<ExManager ErrorCodeSource="DB" ConnectionString="Data Source=PDMDEV151.ITDEV.ZTE.COM.CN;user id=PDM;password=" DataBase="ErrorInfo">

<ExHandler Name="BusinessFacadeLayer" Type="ExManagement.Handler.DefaultExHandler, ExManagement.Handler" ReturnMode="Exception" AlertType="None"/>

<LogHandler Type="ExManagement.LogHandler.DefaultLogHandler, ExManagement.LogHandler" ConnectionString="Data Source=PDMDEV151.ITDEV.ZTE.COM.CN;user id=PDM;password=" DataTable ="ExceptionLog"/>

ExHandler>

ExManager>

  该层配置除ExHandler的Name不同外,与BusinessFacadeLayer的配置基本一致。

  WebUILayer

<ExManager ErrorCodeSource="DB" ConnectionString="Data Source=PDMDEV151.ITDEV.ZTE.COM.CN;user id=PDM;password=" DataBase="ErrorInfo">

<ExHandler Name="WebUILayer" Type="ExManagement.Handler.DefaultExHandler, ExManagement.Handler" ReturnMode="ExceptionString" AlertType="WebUI"/>

<LogHandler Type="ExManagement.LogHandler.DefaultLogHandler, ExManagement.LogHandler" ConnectionString="Data Source=PDMDEV151.ITDEV.ZTE.COM.CN;user id=PDM;password=" DataTable ="ExceptionLog"/>

ExHandler>

ExManager>

  该层的异常处理器的ReturnMode方式为ExceptionString(即Exception.Message),弹出提示方式为WebUI。

  WinUILayer

<ExManager ErrorCodeSource="DB" ConnectionString="Data Source=PDMDEV151.ITDEV.ZTE.COM.CN;user id=PDM;password=" DataBase="ErrorInfo">

<ExHandler Name="WinUILayer" Type="ExManagement.Handler.DefaultExHandler, ExManagement.Handler" ReturnMode="ExceptionString" AlertType="WinUI"/>

<LogHandler Type="ExManagement.LogHandler.DefaultLogHandler, ExManagement.LogHandler" ConnectionString="Data Source=PDMDEV151.ITDEV.ZTE.COM.CN;user id=PDM;password=" DataTable ="ExceptionLog"/>

ExHandler>

ExManager>

原文转自:http://kb.cnblogs.com/page/81682/