Microsoft Enterprise Library 6.0 之 Exception 企业库异常处理

对于企业库异常处理,这里做个简单的介绍和笔记。

环境

VS2012, .NET Framework 4.0, Microsoft Enterprise Library 6.0

准备工作

1. 下载Enterprise Library配置编辑工具:Microsoft.Practices.EnterpriseLibrary.ConfigConsoleV6.vsix。

下载地址:http://www.microsoft.com/en-us/download/details.aspx?id=38789

2. 通过 NuGet 添加所需要的企业库模块。

异常处理需要添加Microsoft Enterprise Library的ExceptionHandling,如果要写Log就需要添加ExceptionHandling.Logging 模块。

2.1 右键点击需要使用Exception模块的项目,选择Manage NuGet Packeages。或者选择Tools-> Library Package Manager -> Manage NuGet Packages for Solution。

2.2 在Manage NuGet Packeages窗口里面Online查找Enterprise Library,点击需要安装需要的模块。

3. 安装成功后,所需要的DLL Reference会自动添加。

如何使用

1. 右键App.config文件选择Edit configuration file v6配置App.config文件。

2. 添加如下图的配置。

FormatException的Post handle配置为None。FormatException将不会被处理。

FileNotFoundException的Post handle配置为ThrowNewException。FileNotFoundException将被包装为TimeoutException。

NullReferenceException的Post handle配置为NotifyRethrow,并且配置了写入log file.

3. 测试代码如下。已经做了注释,这里就不多讲了。

static void Main(string[] args)
{
    TestException();
}        

private static void TestException()
{
    bool hasFormatException = false;
    bool hasNullReferenceException = false;
    bool hasFileNotFoundException = false;
    bool hasTimeoutException = false;

    //Enterprise Lib 5.0
    //ExceptionManager em = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();

    //Enterprise Lib 6.0
    Logger.SetLogWriter(new LogWriterFactory().Create());
    IConfigurationSource config = ConfigurationSourceFactory.Create();
    ExceptionPolicyFactory factory = new ExceptionPolicyFactory(config);
    ExceptionManager em = factory.CreateManager();

    //FormatException异常捕获设置为None,将不会被抛出
    try
    {
        em.Process(NotThrowException, "Policy");
    }
    catch (FormatException)
    {
        hasFormatException = true;
        Console.WriteLine("捕获到FormatExceptio异常!");
    }
    if (hasFormatException == false)
    {
        Console.WriteLine("未捕获到FormatExceptio异常!");
    }

    //NullReferenceException异常捕获设置为NotifyThrow,将会被抛出
    try
    {
        em.Process(NotifyThrowException, "Policy");
    }
    catch (NullReferenceException)
    {
        hasNullReferenceException = true;
        Console.WriteLine("捕获到NullReferenceException异常!");
    }

    if (hasNullReferenceException == false)
    {
        Console.WriteLine("未捕获到NullReferenceException异常!");
    }

    //FileNotFoundException异常捕获设置为ThrowNewException,将会被抛出
    try
    {
        em.Process(ThrowNewException, "Policy");
    }
    catch (FileNotFoundException)
    {
        hasFileNotFoundException = true;
        Console.WriteLine("捕获到FileNotFoundException异常!");
    }
    catch (TimeoutException)
    {
        hasTimeoutException = true;
        Console.WriteLine("捕获到TimeoutException异常!");
    }

    if (hasFileNotFoundException == false)
    {
        Console.WriteLine("未捕获到FileNotFoundException异常!");
    }
    if (hasTimeoutException == false)
    {
        Console.WriteLine("未捕获到TimeoutException异常!");
    }
    Console.ReadLine();
}

private static void NotThrowException()
{
    throw new FormatException();
}

private static void NotifyThrowException()
{
    throw new NullReferenceException();
}

private static void ThrowNewException()
{
    throw new FileNotFoundException();
}
时间: 2024-08-03 22:27:19

Microsoft Enterprise Library 6.0 之 Exception 企业库异常处理的相关文章

转:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)

http://www.360doc.com/content/13/0918/22/15643_315482318.shtml http://www.360doc.com/content/13/0918/22/15643_315483614.shtml http://www.cnblogs.com/huangcong/archive/2010/05/28/1745909.html http://www.cnblogs.com/happyhippy/archive/2010/08/15/180028

Microsoft Enterprise Library 简介与请大家下载Microsoft Enterprise Li

什么是Enterprise Library     Enterprise Library是一组应用程序块(Application Block)的集合.他们是可重用的软件组件,被设计用来帮助开发者面对常用的企业级开发任务.用来解决我们在企业级开发中遇到常见问题,如配置管理.数据访问.缓存管理.记录操作日志.异常管理.加密解密.权限管理等.它是对很多有影响力的软件企业通过多年的开发实践积累下来的技术的整合当.前的版本是2006年1月发布的,基于.NET framework 2.0.这个版本包含了大量

Enterprise Library 5.0 系列教程

1. Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (初级) 2. Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级) 3. Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级) 4. Microsoft Enterpr

Microsoft Enterprise Library操作数据库的应用

下载Microsoft Enterprise Library 5.0:http://www.microsoft.com/en-us/download/details.aspx?id=15104,安装包Enterprise Library 5.0.msi,选择目录后安装. 1.引用dll安装后在安装目录下找到以下dll: 2.基本用法 Database db = null; DbCommand cmd = db.GetSqlStringCommand(sqlGetUserInfo); db.Add

Enterprise Library 6.0 Semantic Logging Application Block Configuration

使用Enterprise Library 6.0的Logging application 模块,配置步骤如下: 1.Nuget 安装 Enterprise Library Logging模块 命令行:Install-package EnterpriseLibrary.Logging. 2.配置文件: 当前基本都是通过Enterprise Library 配置的,但是很不幸,我的总是安装失败,于是自己baidu了一把,然后进行配置,配置如下: <configSections> <secti

Microsoft Enterprise Library 之 Log 的使用

IDE: VS2012 Framework: 4.0 1. 添加Microsoft Enterprise Library的Logging Application模块.右键点击需要使用Log功能的项目,选择Manage NuGet Packeages. 2. 在Manage NuGet Packeages窗口里面Online查找Enterprise Library - Logging Application Block,点击安装. 3. 安装成功后,Enterprise Library Commo

Enterprise Library 5.0 学习笔记

最近了解了微软提供的企业开发框架Enterprise Library,目前最新版本是6.0,但是不支持FW3.5,所以就学习了5.0的版本,EL5.0支持FW3.5和4.0,官网下载地址是:https://www.microsoft.com/en-us/download/details.aspx?id=15104,将msi文件解压到特定的文件夹就可以有EL5.0的全部dll类库了,EL5.0的文档下载地址是:http://entlib.codeplex.com/releases/view/431

转:Enterprise Library 4.0缓存应用程序块

英文原文:http://msdn.microsoft.com/zh-cn/library/cc511588(en-us).aspx Enterprise Library 缓存应用程序块允许开发人员在应用程序中合并一个局部缓存,它支持内存内的缓存,和可选的可以是数据库存储或独立存储的后端存储.应用程序块可以不做修改 的使用,它提供所有必须的获取.添加和移除缓存数据的功能.可配置的到期和清除策略也是应用程序块的一部分. 在构建企业范围发布的应用程序时,架构和开发人员都要面对许多挑战,缓存可以帮助他们

微软企业库的Cache

微软企业库的Cache 通常,应用程序可以将那些频繁访问的数据,以及那些需要大量处理时间来创建的数据存储在内存中,从而提高性能.基于微软的企业库,我们的快速创建一个缓存的实现. 新建PrismSample.Infrastructure.Cache 新建一个类库项目,将其命名为PrismSample.Infrastructure.Cache,然后从nuget中下载微软企业库的Cache. 然后新建我们的CacheManager类: using Microsoft.Practices.Enterpr