使用异常管理应用程序块Exception Management Application Block
2004-11-03 16:02
453 查看
异常管理应用程序块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,就介绍到这吧。
异常管理应用程序块提供了管理应用程序异常的灵活而又简单的方法。先来看一个最简单使用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,就介绍到这吧。
相关文章推荐
- 使用异常管理应用程序块Exception Management Application Block
- 使用autotools进行项目管理简单应用
- 转:使用xinetd管理网络应用服务
- 使用Eclipse构建Maven管理的Struts2应用
- WCF 第十章 异常处理 使用FaultException管理服务异常
- FreeBSD 6.0架设管理与应用-第十一章X Window 的使用
- 使用内嵌资源组织管理系统异常信息
- 在python中使用zookeeper管理你的应用集群
- 使用内容管理系统做门户网站——JeeCMS应用实践和二次开发
- 使用分区表来提高应用系统的性能并方便数据管理
- 使用 Osql 工具管理 SQL Server 桌面引擎 (MSDE 2000)应用介绍
- Java 应用异常使用简单原则
- OpenShift Redhat免费空间SSH登录管理和使用:下载文件安装程序和应用
- 负载均衡之---应用请求路由模块的使用(ARR)(六)[使用ARR管理试点方案(涉及到了A/B Testing)]
- 使用WMI筛选器管理组策略应用
- web developer tips (36):使用IIS7.0 应用请求路由模块管理网站的beta版程序
- Vaadin Web应用开发教程(44): 使用Container接口管理一组Item
- 使用WMI筛选器管理组策略应用
- Citrix无缝窗口模式下应用使用密码管理会自动退出
- 一些linux应用小技巧,网络相关,系统管理,shell,程序使用