• 微软企业库5.0学习笔记(十四)


    使用企业库异常处理应用程序模块的优势:

    1. 它支持整个应用程序体系结构层的异常处理,而不仅限于服务接口的界限。
    2. 它使得异常处理策略可以在管理层定义和维护,以便决策人员(可能是系统管理员和开发人员)可以定义如何处理异常。他们可以维护和修改控制异常处理的规则集,而无需更改块的应用程序代码。
    3. 它提供了常用的异常处理功能,例如记录异常信息的功能、通过将原始异常替 换为其他异常来隐藏敏感信息的功能,以及通过将原始异常打包到另一个异常中来添加异常的上下文信息的功能。这些功能封装在名为 Exception handlers 的 .NET 类中。
    4. 它可以合并多个异常处理程序以产生某个异常所需的响应,例如先记录异常信 息,再将原始异常替换为其他异常。
    5. 它使开发人员能够创建自己的异常处理程序。
    6. 它以一致的方式调用异常处理程序。这意味着,处理程序可以在应用程序之中 和之间的多种场合下使用。

     

    下面我们来试试看用EL5.0的异常处理模块到底能做些什么,按需求来做分析是最好的办法,那我们可以模拟的提出下列几个需求,看看用异常处理模块如何灵活的解决它们:

    <!--[if !supportLists]-->1.       <!--[endif]-->希望能过滤程序中某些异常,即在发生这些异常的时候不会被抛出.要求是配置简单,只用修改一处地方就能控制所有相应的异常.

    <!--[if !supportLists]-->2.       <!--[endif]-->发生某种异常的时候被自动替换成另外一个异常

    <!--[if !supportLists]-->3.       <!--[endif]-->发生某种异常的时候被自动包装到另外一个异常中

    <!--[if !supportLists]-->4.       <!--[endif]-->发生某种异常的时候被自动记录在指定的日志记录策略中,可以是记录到数据库或者文件中.

     

    下面介绍如何使用Microsoft Enterprise Library 5.0中的异常处理程序模块来处理上面的问题:

    1.      运行EntLibConfig.exe, 选择Blocks菜单 ,单击 Add Exception Handling Settings .

     


    <!--[if !vml]-->

    <!--[endif]-->

    2.      为了模拟第一个问题,我们要先删除原有的All Exceptions,因为它表示所有的异常均截取,我们删除了它,再在Policy面板上右键—Add Exception Type,在弹出的异常类型选择窗口中,我们选择一个异常System.FormatException:

     


    <!--[if !vml]-->

    <!--[endif]-->

    <!--[if !vml]--><!--[endif]-->

     

    <!--[if !supportLists]-->3.       <!--[endif]-->点击 File 菜单,单击 Save,保存为一个App.config文件,可以先保存到桌面,之后要用到它.

     

    <!--[if !supportLists]-->4.       <!--[endif]-->创建一个新的控制台应用程序,将App.config添加到程序内,并加入需要的Dll文件,并添加需要的引用:

    <!--[if !vml]--><!--[endif]-->

     

     

    添加引用:

     

    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;

     

    测试:
    代码

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Collections;
    using System.Collections.Specialized;

    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;


    namespace test
    {
    class Program
    {
    static void Main(string[] args)
    {
    bool haveException = false;

    ExceptionManager em = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();

    try
    {
    //FormatException异常捕获设置为None,将不会被抛出
    em.Process(NoThrowException, "Policy");
    }
    catch (Exception)
    {
    haveException = true;
    Console.WriteLine("捕获到异常!");
    }

    if (haveException == false)
    {
    Console.WriteLine("未捕获到任何异常!");
    }
    }

    private static void NoThrowException()
    {
    int i = Int32.Parse("A");
    Console.WriteLine("发生异常,不执行该指令");
    }
    }
    }

     

     

    运行结果:

     


    <!--[if !vml]-->

    <!--[endif]-->

    到此为止,我们已经解决了第一个问题,当你的程序想只过滤System.FormatException异常的时候,可以用上面的方法实现,如果想过滤其他的异常,只用在EL5中添加即可,无需更改程序中的任何代码.

     

    <!--[if !supportLists]-->5.       <!--[endif]-->下面我们来解决第二个问题,让我们回到EL5.0,在Policy面板上右键—Add Exception Type,在弹出的异常类型选择窗口中,我们选择一个异常System.IO. FileNotFoundException,再在FileNotFoundException面板上右键—Add HandlersAdd Replace Handler,在建立好的Replace Handler面板中点击Replace Exception Type右边的…按钮,选择要替换成的异常类型,我们选择:System.TimeputException:

     


    <!--[if !vml]-->

    <!--[endif]-->

    <!--[if !supportLists]-->6.       <!--[endif]-->Save一下,更新App.config文件,修改源程序如下:

    代码

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Collections;
    using System.Collections.Specialized;

    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;


    namespace test
    {
    class Program
    {
    static void Main(string[] args)
    {
    //bool haveException = false;

    ExceptionManager em = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();

    //try
    //{
    // //FormatException异常捕获设置为None,将不会被抛出
    // em.Process(NoThrowException, "Policy");
    //}
    //catch (Exception)
    //{
    // haveException = true;
    // Console.WriteLine("捕获到异常!");
    //}

    //if (haveException == false)
    //{
    // Console.WriteLine("未捕获到任何异常!");
    //}

    //FileNotFoundException异常捕获被设置为ThrowNewException,将被替换为TimeoutException异常
    try
    {
    em.Process(ReplaceHandler, "Policy");
    }
    catch (TimeoutException e)
    {
    Console.WriteLine("捕获到TimeoutException异常!异常信息:" + e.Message);
    }
    }

    private static void ReplaceHandler()
    {
    File.Open("test.txt", FileMode.Open);
    Console.WriteLine("发生异常,不执行该指令");
    }

    private static void NoThrowException()
    {
    int i = Int32.Parse("A");
    Console.WriteLine("发生异常,不执行该指令");
    }
    }
    }

     

     

    <!--[if !supportLists]-->7.       <!--[endif]-->运行结果:

     

    <!--[if !vml]--><!--[endif]-->

    到此为止我们便解决了第二个问题,是不是很简单呀,这就是EL的便利之处~

     

    <!--[if !supportLists]-->8.       <!--[endif]-->接着处理第三个问题,回到EL, 在Policy面板上右键—Add Exception Type,在弹出的异常类型选择窗口中,我们选择一个异常System. NullReferenceException,再在NullReferenceException面板上右键—Add Handlers—Add Wrap Handler,在建立好的Wrap Handler面板中点击Wrap Exception Type右边的…按钮,选择要替换成的异常类型,我们选择: System.ApplicationException:

    <!--[if !vml]--><!--[endif]-->

     

    <!--[if !supportLists]-->9.       <!--[endif]-->Save一下,更新App.config文件,修改源程序如下:

     

    代码

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Collections;
    using System.Collections.Specialized;

    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;


    namespace test
    {
    class Program
    {
    static void Main(string[] args)
    {
    //bool haveException = false;

    ExceptionManager em = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();

    //try
    //{
    // //FormatException异常捕获设置为None,将不会被抛出
    // em.Process(NoThrowException, "Policy");
    //}
    //catch (Exception)
    //{
    // haveException = true;
    // Console.WriteLine("捕获到异常!");
    //}

    //if (haveException == false)
    //{
    // Console.WriteLine("未捕获到任何异常!");
    //}

    //Console.WriteLine("-----------------------------------------------");
    ////FileNotFoundException异常捕获被设置为ThrowNewException,将被替换为TimeoutException异常
    //try
    //{
    // em.Process(ReplaceHandler, "Policy");
    //}
    //catch (TimeoutException e)
    //{
    // Console.WriteLine("捕获到TimeoutException异常!异常信息:" + e.Message);
    //}

    //Console.WriteLine("-----------------------------------------------");
    ////NullReferenceException异常捕获被设置为ThrowNewException,将被包装为ApplicationException异常
    try
    {
    em.Process(WrapHandler, "Policy");
    }
    catch (ApplicationException e)
    {
    Console.WriteLine("捕获到ApplicationException异常,其被包装的异常为{0}!", e.InnerException.GetType().ToString());
    Console.WriteLine("异常信息:{0}", e.Message);
    }
    }

    private static void WrapHandler()
    {
    Hashtable ht = new Hashtable();
    ht["test"].ToString();
    Console.WriteLine("发生异常,不执行该指令");
    }

    private static void ReplaceHandler()
    {
    File.Open("test.txt", FileMode.Open);
    Console.WriteLine("发生异常,不执行该指令");
    }

    private static void NoThrowException()
    {
    int i = Int32.Parse("A");
    Console.WriteLine("发生异常,不执行该指令");
    }
    }
    }

     

     

     

    <!--[if !supportLists]-->10.   <!--[endif]-->运行结果:

     


    <!--[if !vml]-->

    <!--[endif]-->到此为止,我们又解决了第三个问题.

     

    <!--[if !supportLists]-->11.   <!--[endif]-->接着处理第四个问题,回到EL, 在Policy面板上右键—Add Exception Type,在弹出的异常类型选择窗口中,我们选择一个异常System. NullReferenceException,再在NullReferenceException面板上右键—Add Handlers—Add Logging Exception Handler,将日志策略设置为文件记录方式,详细步骤在此不多讲,大家可以看看我之前写的日志处理模块教程:


    <!--[if !vml]-->

    <!--[endif]-->

    <!--[if !supportLists]-->12.   <!--[endif]-->Save一下,更新App.config文件,修改源程序如下:

     

    代码

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Collections;
    using System.Collections.Specialized;

    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;


    namespace test
    {
    class Program
    {
    static void Main(string[] args)
    {
    //bool haveException = false;

    ExceptionManager em = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();

    //try
    //{
    // //FormatException异常捕获设置为None,将不会被抛出
    // em.Process(NoThrowException, "Policy");
    //}
    //catch (Exception)
    //{
    // haveException = true;
    // Console.WriteLine("捕获到异常!");
    //}

    //if (haveException == false)
    //{
    // Console.WriteLine("未捕获到任何异常!");
    //}

    //Console.WriteLine("-----------------------------------------------");
    ////FileNotFoundException异常捕获被设置为ThrowNewException,将被替换为TimeoutException异常
    //try
    //{
    // em.Process(ReplaceHandler, "Policy");
    //}
    //catch (TimeoutException e)
    //{
    // Console.WriteLine("捕获到TimeoutException异常!异常信息:" + e.Message);
    //}

    //Console.WriteLine("-----------------------------------------------");
    ////NullReferenceException异常捕获被设置为ThrowNewException,将被包装为ApplicationException异常
    //try
    //{
    // em.Process(WrapHandler, "Policy");
    //}
    //catch (ApplicationException e)
    //{
    // Console.WriteLine("捕获到ApplicationException异常,其被包装的异常为{0}!", e.InnerException.GetType().ToString());
    // Console.WriteLine("异常信息:{0}", e.Message);
    //}

    //Console.WriteLine("-----------------------------------------------");
    //捕获ArgumentOutOfRangeException异常,并写入日志
    try
    {
    em.Process(NotifyRethrow, "Policy");
    }
    catch (ArgumentOutOfRangeException)
    {
    Console.WriteLine("捕获到ArgumentOutOfRangeException异常,并写入日志!");
    }
    }

    private static void NotifyRethrow()
    {
    List<string> list = new List<string>();
    string str = list[1];
    Console.WriteLine("发生异常,不执行该指令");
    }

    private static void WrapHandler()
    {
    Hashtable ht = new Hashtable();
    ht["test"].ToString();
    Console.WriteLine("发生异常,不执行该指令");
    }

    private static void ReplaceHandler()
    {
    File.Open("test.txt", FileMode.Open);
    Console.WriteLine("发生异常,不执行该指令");
    }

    private static void NoThrowException()
    {
    int i = Int32.Parse("A");
    Console.WriteLine("发生异常,不执行该指令");
    }
    }
    }

     

     

     

    <!--[if !supportLists]-->13.   <!--[endif]-->运行结果:

     


    <!--[if !vml]-->

    <!--[endif]-->

    打开工程目录下的rolling.log文件:

    <!--[if !vml]--><!--[endif]-->

    OK,到此为止,我们将四个问题都解决了.

     

     

    测试程序可以点击这里下载 \(^  ^ \)

    请输入标

    出处: http://www.cnblogs.com/huangcong/archive/2010/06/03/1751087.html 

  • 相关阅读:
    解决IE 下div与img重叠无法触发鼠标事件的问题
    四边相同阴影效果
    dedecms 获取文章发布时间和获取文章最后更新时间
    局域网访问网站
    HTML 5 的data-* 自定义属性
    yum 安装 influxdb/telegraf
    zabbix 监控 AWS-SQS 队列
    解决阿里云部署 office web apps ApplicationFailedException 报错问题
    jira集成fisheye代码深度查看工具安装绿色版
    阿里云ecs开启x11图形化桌面
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/1751380.html
Copyright © 2020-2023  润新知