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();
}
posted @ 2015-01-28 17:28  李泽鑫  阅读(1026)  评论(0编辑  收藏  举报