5.4. Summary

This chapter reviewed the options available for handling exceptions in a .NET application. It used the Microsoft Enterprise Application blocks to implement exception handling, logging, and e-mailing. Typically, you set this up once and never touch it again, but you do have the flexibility to add or change how exceptions are handled after the project is deployed.

Remember these key points when setting up exception handling:

  • For display to users, add a common page that hides the details of the exception; and give users a friendly message (as friendly as an error message can be).

  • Add the customErrors section to the web.config file, setting the mode to RemoteOnly and the defaultRedirect to the custom error page.

  • Add the appropriate references to the Microsoft Application Blocks.

  • Configure the web.config file using the Enterprise Library Configuration tool.

  • Add a Logging Application Block section to the web.config file and add an e-mail trace listener. Hook the All Events special source to use the e-mail trace listener.

  • Add an Exception Handling Block section to the web.config file and add a new policy that catches the generic exception type, and add a logging handler to the exception type.

  • Add a Global.asax file to the project and add the ExceptionPolicy.Handle Exception(Server.GetLastError(), "Global Policy"); line to the Application_Error event.

  • Customize the error message by editing the Template property of the TextFormatter node in the web.config file.

The previous four ...

Get ASP.NET 3.5 Enterprise Application Development with Visual Studio® 2008: Problem - Design - Solution now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.