黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block

原文: 黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block

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

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

 

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

1.       但愿能过滤程序中某些异常,即在发生这些异常的时候不会被抛出.要求是配置简单,只用修改一处地方就能控制全部相应的异常.ide

2.       发生某种异常的时候被自动替换成另一个异常post

3.       发生某种异常的时候被自动包装到另一个异常中测试

4.       发生某种异常的时候被自动记录在指定的日志记录策略中,能够是记录到数据库或者文件中.url

 

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

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


 

 

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


 

 

 

3.       点击 File菜单,单击 Save,保存为一个App.config文件,能够先保存到桌面,以后要用到它.code

 

4.       建立一个新的控制台应用程序,App.config添加到程序内,并加入须要的Dll文件,并添加须要的引用:

 

添加引用:

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(
" 发生异常,不执行该指令 " );
}
}
}
 

 

运行结果:


 

 

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

 

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


 

 

6.       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(
" 发生异常,不执行该指令 " );
}
}
}

 

7.       运行结果:


 

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

 

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

 

9.       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(
" 发生异常,不执行该指令 " );
}
}
}
 

 

10.   运行结果:


 

 到此为止,咱们又解决了第三个问题.

 

11.   接着处理第四个问题,回到EL,Policy面板上右键—Add Exception Type,在弹出的异常类型选择窗口中,咱们选择一个异常System. NullReferenceException,再在NullReferenceException面板上右键—Add Handlers—Add Logging Exception Handler,将日志策略设置为文件记录方式,详细步骤在此很少讲,你们能够看看我以前写的日志处理模块教程: 


 

 

12.   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(
" 发生异常,不执行该指令 " );
}
}
}
 

 

13.   运行结果:


 

 

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


 

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

 

 

测试程序能够点击这里下载\(^^ \)

相关文章
相关标签/搜索