使用Microsoft Enterprise Library 3.1 (May,2007) Exception Handling Block的一点经验

想做个Exception记录,把程序运行中的所有Exception记录下来。原来是自己写了一个简单的类,把异常信息写到文本文件里面。后来看到microsoft enterprise library中提供此功能,而且可以灵活的加以配置,就决定使用它。

看了一个微软自己带的例程,感觉使用也很简单。就自己用了。结果却让遇到莫名其妙的问题,我费了好多功夫才把这问题搞好。 

我以前使用enterprise library时,主要使用DataAccess部分,也没有仔细对Enterprise Libarry整体和配置文件等方面内容进行仔细研究。在使用Exception Handling Block时,遇到莫名其妙的错误如下 :

 The type 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=3.1.0.0, Culture=neutral,  PublicKeyToken=b03f5f7f11d50a3a'  cannot be resolved. Please verify the spelling is correct or that the full type name is provided.

查自己的代码,与微软的例程完全一样。从错误提示来看,可能是找不到程序集,或者版本不对。于是又仔细核对以确认添加的引用版本没有问题。

经过几次折腾,最后发现原因在于:(1)要添加正确、完整的程序集引用 (2)要在配置文件中进行正确配置。

     * 需要添加的程序集引用包括:Microsoft.Practise.EnterpriseLibrary.ExceptionHandling
     * 和Microsoft.Practise.EnterpriseLibrary.ExceptionHandling.Logging
     * 这两个程序集是在GAC中,显示名称为:Enterprise Library Exception Handling Application Block
     * 和Enterprise Library Exception Handling Logging Provider

配置文件中的内容多而且复杂,不适合直接修改,我是用微软提供的Enterprise Library Configuration工具进行修改的。以可视化的形式,以元素和属性的形式进行修改,屏蔽掉具体的语法细节,方便多了。

最后一点需要说明的是

如果你在Utility项目中写了一个处理异常的类,我的类叫做ExceptionFacade,你又在别的项目中(比如MessageServer)引用此Utility项目,那么你的引用项目MessageServer也要添加对于上述程序集的引用,也要在配置文件中加以配置。

发布了14 篇原创文章 · 获赞 4 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/greatwall_sdut/article/details/1649483