Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)

练习1 记录异常信息
通过本练习,将会在一个没有异常处理的应用程序中使用异常处理应用程序块添加本地和全局的异常处理,并记录到Windows事件日志中。
 
第一步
打开Puzzler.sln 项目,默认的安装路径应该为C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Exception Handling\exercises\ex01,并编译。
 
第二步 回顾应用程序
选择Debug | Start Debugging菜单命令运行应用程序,当前应用程序并没有出现异常信息。当尝试增加一个带有数字的单词(在文本框中输入“abc123”并单击Add Word按钮)到目录中时,将会出现一个未处理的异常,调试将会中断。
选择Debug | Stop Debugging菜单命令退出应用程序并返回Visual Studio
 
第三步 增加Try/Catch异常处理
1.选择PuzzlerUI项目,并选择Project | Add Reference …菜单命令,选择Browse项并添加如下程序集。
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)默认的安装位置为C:\Program Files\Microsoft Enterprise Library January 2006\bin
2.在解决方案管理器中选择Puzzler.cs文件,并选择View | Code菜单命令,添加如下命名空间。
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
3.在btnAddWord_Click方法中添加如下代码,在调用AddWord SetError时添加try/catch区。
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)private void btnAddWord_Click(object sender, System.EventArgs e)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
{
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    
try
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    
{
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)        
// TODO: Handle exceptions
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)

Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)        PuzzlerService.Dictionary.AddWord(txtWordToCheck.Text);
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)        errorProvider1.SetError(txtWordToCheck, 
"");
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    }

Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    
catch (Exception ex)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    
{
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)        
bool rethrow = ExceptionPolicy.HandleException(ex, "UI Policy");
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)        
if (rethrow)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)            
throw;
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)        MessageBox.Show(
string.Format(
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)            
"Failed to add word {0}, please contact support.",
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)            txtWordToCheck.Text));
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    }

Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)}
注意这里使用throw语句非常重要,而不能使用throw ex。如果使用throw ex将在重新抛出异常点对异常信息重新进行包装,这样达不到预期的效果。
 
第四步 企业库配置工具
1.在项目PuzzlerUI中添加一个新的应用程序配置文件(App.config)。单击PuzzlerUI项目,选择Project| Add New Item…菜单命令,然后选择Application configuration file模版,保留App.config名字。
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)2.使用Enterprise Library配置工具配置应用程序,可以通过开始菜单打开该配置工具,选择所有程序| Microsoft patterns and practices | Enterprise Library | Enterprise Library Configuration,并打开App.config文件。或者直接在Visual Studio中使用该工具打开配置文件。
3.在解决方案管理器中选中App.config文件,在View菜单或者在右键菜单中选择Open With…,将打开OpenWith对话框,单击Add按钮。
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)4.在Add Program对话框中,设置Program name指向EntLibConfig.exe文件,默认的路径为C:\Program Files\Microsoft Enterprise Library January 2006\bin,设置Friendly nameEnterprise Library Configuration,单击OK按钮。
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)Visual Studio会把配置文件(App.config)作为一个命令行参数传递给EntLibConfig.exe
5.在Open With对话框中,选中Enterprise Library Configuration并单击OK按钮。
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一) 
第五步 配置应用程序以使用异常管理
1.右击应用程序并选择New | Exception Handling Application Block
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)2.选中Exception Handling Application Block节点,选择Action | New | Exception Policy菜单命令,设置Name属性为UI Policy
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)3.选中UI Policy节点,选择Action | New | Exception Type菜单命令,将会打开Type Selector对话框,选择System.Exception(默认)并单击OK按钮。
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)4.选中Exception节点,设置属性PostHandlingActionNone
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)这将会使所有的异常都会被异常处理代码所处理。
5.选中Exception Handling Application Block | UI Policy | Exception节点,选择Action | New | Logging Handler菜单命令。
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)注意通过这步操作之后将会自动包含日志应用程序块到配置中来。
6.选择Exception Handling Application Block | UI Policy | Exception | Logging Handler节点,并设置如下属性。
FormatterType = TextExceptionFormatter
LogCategory = General
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)7.选择菜单File | Save All保存应用程序的配置,并关闭Enterprise Library Configuration工具。
 
第六步 加入异常记录程序集
选择项目PuzzlerUI,选择Project | Add Reference …菜单命令,在弹出的对话框中选中Browse项并加入如下程序集。
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.dll
 
第七步 运行应用程序
1.选择Debug | Start Without Debugging菜单命令运行应用程序。在Word to check文本框中输入数字并单击Add Word按钮。将会弹出一个错误信息提示框,显示的信息为“Failed to add word …, please contact support”。
2.打开Windows事件查看器,查看应用程序日志,异常信息已经被记录。Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
3.关闭应用程序
 
第八步 添加全局异常处理
1.虽然可以在每一个事件处理的地方都加入try/catch程序块来处理异常,但通常更好的方法是加入一个全局的异常处理不管任何时候发生异常都会被处理。
2.在解决方案管理器中选择Puzzler.cs文件,选择View | Code菜单命令。在方法btnAddWord_Click中移除前面添加的异常处理程序,使其变为如下的代码。
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)private void btnAddWord_Click(object sender, System.EventArgs e)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
{
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    PuzzlerService.Dictionary.AddWord(txtWordToCheck.Text);
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    errorProvider1.SetError(txtWordToCheck, 
"");
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)}
3.在解决方案管理器中选择Startup.cs文件,选择View | Code菜单命令,添加如下的命名空间。
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
应用程序的入口点,这里提供了两个事件可以用来处理异常信息。当一个未处理的异常发生在主工作线程上时Application.ThreadException事件被触发,当异常发生在不同于UI的线程上时AppDomain.UnhandledException将会被触发。
4.添加如下异常处理程序到Startup类中。
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)public static void HandleException(Exception ex, string policy)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
{
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    Boolean rethrow 
= false;
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    
try
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    
{
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)        rethrow 
= ExceptionPolicy.HandleException(ex, policy);
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    }

Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    
catch (Exception innerEx)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    
{
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)        
string errorMsg = "An unexpected exception occured while " +
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)            
"calling HandleException with policy '" + policy + "'. ";
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)        errorMsg 
+= Environment.NewLine + innerEx.ToString();
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)        MessageBox.Show(errorMsg, 
"Application Error",
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)            MessageBoxButtons.OK, MessageBoxIcon.Stop);
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)        
throw ex;
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    }

Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    
if (rethrow)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    
{
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)        
// WARNING: This will truncate the stack of the exception
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)

Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)        
throw ex;
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    }

Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    
else
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    
{
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)        MessageBox.Show(
"An unhandled exception occurred and has " +
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)            
"been logged. Please contact support.");
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    }

Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)}
该方法用于异常处理程序块,它用于当异常处理程序块本身出现问题时(比如说配置错误)显示错误信息。
5.添加如下代码到应用程序的ThreadException事件中。
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
{
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    HandleException(e.Exception, 
"UI Policy");
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)}
该事件处理将使用UI Policy策略,在配置文件中定义的。
6.为AppDomain UnhandledException添加如下代码。
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
{
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    
if (e.ExceptionObject is System.Exception)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    
{
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)        HandleException((System.Exception)e.ExceptionObject, 
"Unhandled Policy");
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    }

Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)}
该事件处理将使用一个新的名为Unhandled Policy的处理策略,在下一个练习中将会设置它,它只能被记录而不允许重新被抛出。
7.把事件处理与事件之间连接起来,在Main方法中加入如下代码。
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)static void Main()
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
{
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    
// TODO: Handle unhandled exceptions
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)

Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    Application.ThreadException 
+=
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)        
new ThreadExceptionEventHandler(Application_ThreadException);
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    AppDomain.CurrentDomain.UnhandledException 
+=
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)        
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    Puzzler f 
= new Puzzler();
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)    Application.Run(f);
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)
Enterprise Library 2.0 Hands On Lab 翻译(7):异常应用程序块(一)}
8.选择Debug | Start Without Debugging菜单命令运行应用程序。在Word to check文本框中输入数字并单击Add Word按钮。将会弹出一个错误提示信息“An unhandled exception occurred and has been logged. Please contact support.”,可以在事件日志中查看异常信息。
9.关闭应用程序和Visual Studio
 
更多Enterprise Library的文章请参考《Enterprise Library系列文章












本文转自lihuijun51CTO博客,原文链接:http://blog.51cto.com/terrylee/67638 ,如需转载请自行联系原作者