Your Potential, Our Passion!

sharpen(尽其在我)

使用异常管理应用程序块Exception Management Application Block

异常管理应用程序块Exception Management Application Block

异常管理应用程序块提供了管理应用程序异常的灵活而又简单的方法。先来看一个最简单使用EMAB的步骤:
(1)添加组件引用:using Microsoft.ApplicationBlocks.ExceptionManagement;
(2)在捕获异常的时候调用异常管理
                        try
                  {
                        throw new Exception ("测试一个异常");
                  }
                  catch(Exception err)
                  {
                        ExceptionManager.Publish (err);
                        MessageBox.Show (err.Message );
                  }
异常err的信息此时被自动记录到系统事件日志中。

就这么简单!

ExceptionManager.Publish执行的时候,首先会到app.config(或者web.config/machine.config)文件里查

找mode属性设置成on的<exceptionManagement>节点,如果没有设置信息,它会调用默认的异常发布类(发布到事

件日志中)。所以,我们使用上面最简单的发布异常的例子里,我们没有必要在配置文件里做任何配置。

当然,我们也可以显式地在app.config文件中声明使用默认异常发布类:

配置文件的格式如下:
<configuration>
  <configSections>
    <section name="exceptionManagement"
             type="Microsoft.ApplicationBlocks.ExceptionManagement
                   .ExceptionManagerSectionHandler,
                   Microsoft.ApplicationBlocks.ExceptionManagement" />
  </configSections>

  <exceptionManagement mode="on/off">
    <publisher mode="on/off" assembly="AssemblyName" type="TypeName"
               exclude="(+)Type;(+)Type" include="(+)Type;(+)Type"
               exceptionFormat="value" /*xml or others*/
               customattr = "value" />
  </exceptionManagement>
</configuration>

我们来解释一下:
<configSections>
            <section name="exceptionManagement"

type="Microsoft.ApplicationBlocks.ExceptionManagement.ExceptionManagerSectionHandler,Microsoft.Ap

plicationBlocks.ExceptionManagement" />
      </configSections>

上面这个配置表示使用ExceptionManagerSectionHander类来读取exceptionManagement节点注册的Publisher类

<exceptionManagement mode="on">           
            <publisher assembly="Microsoft.ApplicationBlocks.ExceptionManagement"

type="Microsoft.ApplicationBlocks.ExceptionManagement.DefaultPublisher" logname="日志文件名"

applicationname="应用程序名" />
      </exceptionManagement>
这个配置信息表示将默认异常发布类注册成应用程序所使用的异常发布类:发布到事件日志的"日志文件名"文

件里(如果不设logname,则使用"应用程序”日志文件)。

我们可以自定义(一个或多个)异常发布类,并把它注册到app.config中,自定义Publisher类主要是要实现IExceptionPublisher接口的Publish方法。这也是很简单的,大家可以查看Exception Management Application block自带的Sample.

下一步就是把一个或多个异常发布类注册到配置文件中:
<exceptionManagement mode="on">
            <publisher assembly="ExceptionManagementQuickStartSamples" type="ExceptionManagementQuickStartSamples.ExceptionPublisher"  exclude="*" include="ExceptionManagementQuickStartSamples.LogonException, ExceptionManagementQuickStartSamples; ExceptionManagementQuickStartSamples.CustomAppException, ExceptionManagementQuickStartSamples" operatorMail="spli@microsoft.com"/>                       
            <publisher assembly="ExceptionManagementQuickStartSamples" type="ExceptionManagementQuickStartSamples.ExceptionXMLPublisher" exclude="*" include="+Microsoft.ApplicationBlocks.ExceptionManagement.BaseApplicationException, Microsoft.ApplicationBlocks.ExceptionManagement" exceptionFormat="xml" fileName="c:\QuickStartSamplesExceptionLog.xml"/>
      </exceptionManagement>

OK,就介绍到这吧。

posted on 2004-11-03 16:02  sharpen  阅读(1086)  评论(0编辑  收藏  举报

导航