您的位置:首页 > 其它

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

2006-10-07 00:27 531 查看
练习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



默认的安装位置为C:\Program Files\Microsoft Enterprise Library January 2006\bin。
2.在解决方案管理器中选择Puzzler.cs文件,并选择View | Code菜单命令,添加如下命名空间。


using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
3.在btnAddWord_Click方法中添加如下代码,在调用AddWord 和SetError时添加try/catch区。


private void btnAddWord_Click(object sender, System.EventArgs e)








{


try








{


// TODO: Handle exceptions




PuzzlerService.Dictionary.AddWord(txtWordToCheck.Text);




errorProvider1.SetError(txtWordToCheck, "");




}




catch (Exception ex)








{




bool rethrow = ExceptionPolicy.HandleException(ex, "UI Policy");




if (rethrow)




throw;




MessageBox.Show(string.Format(




"Failed to add word {0}, please contact support.",




txtWordToCheck.Text));




}




}
[align=left]注意这里使用throw语句非常重要,而不能使用throw ex。如果使用throw ex将在重新抛出异常点对异常信息重新进行包装,这样达不到预期的效果。[/align]

第四步 企业库配置工具
1.在项目PuzzlerUI中添加一个新的应用程序配置文件(App.config)。单击PuzzlerUI项目,选择Project| Add New Item…菜单命令,然后选择Application configuration file模版,保留App.config名字。


2.使用Enterprise Library配置工具配置应用程序,可以通过开始菜单打开该配置工具,选择所有程序| Microsoft patterns and practices | Enterprise Library | Enterprise Library Configuration,并打开App.config文件。或者直接在Visual Studio中使用该工具打开配置文件。
3.在解决方案管理器中选中App.config文件,在View菜单或者在右键菜单中选择Open With…,将打开OpenWith对话框,单击Add按钮。


4.在Add Program对话框中,设置Program name指向EntLibConfig.exe文件,默认的路径为C:\Program Files\Microsoft Enterprise Library January 2006\bin,设置Friendly name为Enterprise Library Configuration,单击OK按钮。


Visual Studio会把配置文件(App.config)作为一个命令行参数传递给EntLibConfig.exe。
5.在Open With对话框中,选中Enterprise Library Configuration并单击OK按钮。



第五步 配置应用程序以使用异常管理
1.右击应用程序并选择New | Exception Handling Application Block


2.选中Exception Handling Application Block节点,选择Action | New | Exception Policy菜单命令,设置Name属性为UI Policy


3.选中UI Policy节点,选择Action | New | Exception Type菜单命令,将会打开Type Selector对话框,选择System.Exception(默认)并单击OK按钮。


4.选中Exception节点,设置属性PostHandlingActionNone


这将会使所有的异常都会被异常处理代码所处理。
5.选中Exception Handling Application Block | UI Policy | Exception节点,选择Action | New | Logging Handler菜单命令。


注意通过这步操作之后将会自动包含日志应用程序块到配置中来。
6.选择Exception Handling Application Block | UI Policy | Exception | Logging Handler节点,并设置如下属性。
FormatterType = TextExceptionFormatter
LogCategory = General


7.选择菜单File | Save All保存应用程序的配置,并关闭Enterprise Library Configuration工具。

第六步 加入异常记录程序集
选择项目PuzzlerUI,选择Project | Add Reference …菜单命令,在弹出的对话框中选中Browse项并加入如下程序集。


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事件查看器,查看应用程序日志,异常信息已经被记录。


3.关闭应用程序

第八步 添加全局异常处理
1.虽然可以在每一个事件处理的地方都加入try/catch程序块来处理异常,但通常更好的方法是加入一个全局的异常处理不管任何时候发生异常都会被处理。
2.在解决方案管理器中选择Puzzler.cs文件,选择View | Code菜单命令。在方法btnAddWord_Click中移除前面添加的异常处理程序,使其变为如下的代码。


private void btnAddWord_Click(object sender, System.EventArgs e)








{




PuzzlerService.Dictionary.AddWord(txtWordToCheck.Text);




errorProvider1.SetError(txtWordToCheck, "");




}
[align=left]3.在解决方案管理器中选择Startup.cs文件,选择View | Code菜单命令,添加如下的命名空间。[/align]


using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
应用程序的入口点,这里提供了两个事件可以用来处理异常信息。当一个未处理的异常发生在主工作线程上时Application.ThreadException事件被触发,当异常发生在不同于UI的线程上时AppDomain.UnhandledException将会被触发。
4.添加如下异常处理程序到Startup类中。


public static void HandleException(Exception ex, string policy)








{


Boolean rethrow = false;




try








{


rethrow = ExceptionPolicy.HandleException(ex, policy);




}




catch (Exception innerEx)








{


string errorMsg = "An unexpected exception occured while " +




"calling HandleException with policy '" + policy + "'. ";




errorMsg += Environment.NewLine + innerEx.ToString();




MessageBox.Show(errorMsg, "Application Error",




MessageBoxButtons.OK, MessageBoxIcon.Stop);




throw ex;


}






if (rethrow)






{


// WARNING: This will truncate the stack of the exception




throw ex;




}




else








{




MessageBox.Show("An unhandled exception occurred and has " +




"been logged. Please contact support.");




}




}
[align=left]该方法用于异常处理程序块,它用于当异常处理程序块本身出现问题时(比如说配置错误)显示错误信息。[/align]
5.添加如下代码到应用程序的ThreadException事件中。


static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)








{




HandleException(e.Exception, "UI Policy");




}
[align=left]该事件处理将使用UI Policy策略,在配置文件中定义的。[/align]
6.为AppDomain UnhandledException添加如下代码。


static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)








{




if (e.ExceptionObject is System.Exception)








{




HandleException((System.Exception)e.ExceptionObject, "Unhandled Policy");




}




}
[align=left]该事件处理将使用一个新的名为Unhandled Policy的处理策略,在下一个练习中将会设置它,它只能被记录而不允许重新被抛出。[/align]
7.把事件处理与事件之间连接起来,在Main方法中加入如下代码。


static void Main()








{


// TODO: Handle unhandled exceptions




Application.ThreadException +=




new ThreadExceptionEventHandler(Application_ThreadException);




AppDomain.CurrentDomain.UnhandledException +=




new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);




Puzzler f = new Puzzler();




Application.Run(f);




}
[align=left]8.选择Debug | Start Without Debugging菜单命令运行应用程序。在Word to check文本框中输入数字并单击Add Word按钮。将会弹出一个错误提示信息“An unhandled exception occurred and has been logged. Please contact support.”,可以在事件日志中查看异常信息。[/align]
9.关闭应用程序和Visual Studio。

更多Enterprise Library的文章请参考《Enterprise Library系列文章
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐