Enterprise Library-Exception Handling Application Block learning Manual (Latest Version) Part 1

Source: Internet
Author: User
This article demonstrates how to use the Enterprise Library-Exception Handling Application Block Exception management module, how to add exception logs to applications, and how to use replace handler to hide sensitive information. This article by the http://blog.entlib.com open source ASP. Net blog platform team according to the entlib Hol manual compilation to provide, welcome to exchange. Exercise 1: Record abnormal logs1. First open the puzzler. sln project file in the \ Enterprise Library 4.1 Hol \ CS \ Exception Handling \ exercises \ ex01 \ begin directory. This application performs two functions: checking the spelling of words; generating a word list using dictionaries Based on the Character List. Run the application on the following page. In the word text input box, enter ab123 and click Add word to throw an uncaptured exception. The following describes how to use exception handling Application Block to manage exceptions. 2. Select the puzzlerui project and add the reference of the following DLL. Microsoft. Practices. enterpriselibrary. exceptionhandling. dll 3. Open the puzzler. CS code file and add references to the initonhandling namespace. Using Microsoft. practices. enterpriselibrary. exceptionhandling; find the btnaddword_click method and add the try/catch code block, as shown in the following figure: 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) ;}} 4. add an app to the puzzlerui project. config file, and use the configuration file editor that comes with entlib to open the configuration file. Add the configuration of the exception handling Application Block, as shown in.
Next, add a new exception policy, as shown in.
 
Set the name attribute of the exception policy to UI policy. The policy name must be consistent with that in the previous code. Bool rethrow = exceptionpolicy. handleexception (ex, "UI policy"); select the UI Policy node and add a new exception type, as shown in.
In the pop-up type selector dialog box, select system. Exception type, as shown in.
The exception handling code is responsible for processing the exception set by this filter. It will handle all exceptions inherited from the system. Exception type. In addition, set system. the posthandlingaction attribute of the exception type is none. None indicates that all exceptions are processed in the exception handling code. The caller (caller) does not need to throw an exception again in the catch code block. Next, select the exception handling Application Block | UI policy | exception node and select the new | logging handler menu, as shown in. In this step, the logging Application Block is automatically added to the configuration file. You can observe the changes in the configuration file of APP. config.
Select Exception Handling Application Block | UI policy | exception | logging handler node and set the following attributes: formattertype = textexceptionformatterlogcategory = General
 
After completing the configuration, remember to save the configuration file. 5. Set the puzzlerui project to add references to the following DLL. Microsoft. practices. enterpriselibrary. exceptionhandling. logging. DLL is designed to maintain low coupling between application blocks, so Exception Handling Application Block does not need to use logging Application Block (for example, create your own exception management handler ). If you use two application blocks, you need to reference the above DLL, exception handler to record exception logs through the logging Application Block (by default, the DLL is in the following directory c: \ Program Files \ Microsoft Enterprise Library 4.1-October 2008 \ bin ). 6. Run the application again and repeat the previous steps. In the word text input box, enter ABC123, and then click the Add word button. A dialog box-failed to add word ABC123, please contact support is displayed.
Check the windows Event Log, view the application log, and find that the log information has been recorded.
To add try/catch to all codes in the application, you can consider adding a global exception handler. There are two events that can be used to listen for unhandled exceptions: (1) an application. threadexception event is triggered when the thread executing the application. Run method encounters an unhandled exception. (2) If handler throws an exception or does not encounter an exception in the UI thread, the appdomain. unhandledexception event will be triggered. 7. update code (1) Delete the code added in the previous btnaddword_click method, as shown below: Private void btnaddword_click (Object sender, system. eventargs e) {puzzlerservice. dictionary. addword (txtwordtocheck. text); errorprovider1.seterror (txtwordtocheck, "") ;}( 2) Open startup. CS code file to add a namespace reference. Using Microsoft. Practices. enterpriselibrary. exceptionhandling; at the same time, add the following method to the startup class to handle exceptions. 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. ") ;}} the above method uses Exception Handling Application Block. If Exception Handling Application Block has its own problems (such as missing configuration items), it will also display valid information. Add the following method as the event handler of the threadexception event of the application: static void application_threadexception (Object sender, threadexceptioneventargs e) {handleexception (E. exception, "UI policy");} the preceding method uses the previously defined UI policy. In subsequent exercises, this policy will be customized to allow you to ignore some exceptions and close the application. Next, add the event handler: static void currentdomain_unhandledexception (Object sender, unhandledexceptioneventargs e) {If (E. exceptionobject is system. exception) {handleexception (system. exception) E. exceptionobject, "unhandled policy") ;}} the above Code uses a new policy (unhandled policy), which will be created in the next section. The unhandled policy always records exceptions and does not re-throw exceptions. Finally, in the main method, use the following code to connect event handler and event: static void main (){ Application. threadexception + = new threadexceptioneventhandler (application_threadexception ); Appdomain. currentdomain. unhandledexception + = new unhandledexceptioneventhandler (currentdomain_unhandledexception );Puzzler F = new puzzler (); application. run (f);} run the application again, repeat the previous test steps, check the pop-up message dialog box and the exception records in the event log, and confirm that all the above steps have been completed, and the application works as expected. Http://blog.entlib.com open source ASP. NET blog platform team, you are welcome to continue with the method of the next section content: Exception Handling Application Block-Exception management policy. Reference:Exception Handling Application Block hands-on labs for Enterprise Library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.