Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Explore the Enterprise Library Exception Handling Block for .NET 2.0 : Page 4

The Enterprise Library Exception Handling Block provides all the underlying plumbing code required to handle exceptions. Stop writing repetitive exception handling code and learn how to use it in your own applications to make exception handling consistent and effective.


advertisement
Logging an Exception
In addition to handling exceptions, you can also configure the exception handling block to log the exceptions. As mentioned previously, the Exception Handling Block accomplishes this with the help of the Logging Block. To demonstrate logging, add another button named btnLogException and modify its Click event as follows:

private void btnLogException_Click (object sender, EventArgs e) { try { throw new Exception ("This is a test exception"); } catch (Exception ex) { bool rethrow = ExceptionPolicy.HandleException (ex, "Log Only Policy"); if (rethrow) { throw; } } }

The catch block above invokes the ExceptionPolicy.HandleException method by passing the exception object (ex) as well as the policy (the "Log Only Policy" in this case) as arguments. Similar to the exception policies, you also specify logging configuration information in the app.config or web.config file. For example, the app.config code shown below configures the "Log Only Policy" as a child element of the <exceptionPolicies> element:

<add name="Log Only Policy"> <exceptionTypes> <add name="Exception" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="None"> <exceptionHandlers> <add logCategory="Default Category" eventId="100" severity="Error" title="Exception Management Application Exception" priority="0" formatterType="Microsoft. Practices.EnterpriseLibrary. ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary. ExceptionHandling" name="Logging Handler" type="Microsoft.Practices.EnterpriseLibrary. ExceptionHandling.Logging.

 
Figure 3. Event Log Entry: The figure shows a sample application log message as seen through the Windows Event Viewer.
LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary. ExceptionHandling.Logging"/> </exceptionHandlers> </add> </exceptionTypes> </add>
The app.config file that you'll find in the downloadable sample code for this article contains an additional example. For example purposes, the Logging Block simply logs the exceptions in the Windows Application Log, as shown in Figure 3.

Note that the logging configuration information in the app.config file controls the formatting of the log entry.

As you have seen from this article, the exception handling block obviates the need to write the exception handling plumbing code by providing a set of highly reusable classes for handling, logging and processing exceptions. By using these classes, you can reduce errors, bugs, and typos in your application and focus on the core business logic of the application thereby increasing productivity.



Thiru Thangarathinam works at Intel Corporation in Chandler, Arizona. He's a Microsoft MVP who specializes in architecting, designing, and developing distributed enterprise-class applications using .NET-related technologies. He is the author of the books "Professional ASP.NET 2.0 XML" and "Professional ASP.NET 2.0 Databases" from Wrox press and has coauthored a number of books on .NET-related technologies. He is a frequent contributor to leading technology-related online publications.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap