locked
Help setting up Exception Handling/Logging Application Blocks, and an odd Logging issue... RRS feed

  • Question

  • User-1705917722 posted

    Man, I've already spent a couple days looking into this and haven't got very far...

    What is the proper way to use the Exception Handling application block?

    It looks like you setup various Exception Policies, and those policies can handle exceptions in different ways.  So, for example, if you wanted to setup a policy dealing with Data Access, you'd create a DataAccess Exception Policy, add the type of Exceptions you want to handle a certain way, and then in your code write:

    try
    {
    // DataAccess code that could throw an error
    }
    catch (Exception ex)
    {
    bool rethrow = ExceptionPolicy.HandleException(ex, "DataAccess Exception Policy");
    if (rethrow)
    throw;
    }


    And then based on what handlers you have defined for the various exceptions for that policy in the Configuration Tool, the exception can be handled a variety of ways.

    Is this correct?

    You'd still have to add the try...catch block everywhere in your code though, right?

     

     

     

    Friday, September 7, 2007 2:20 PM

All replies

  • User680737545 posted

    Hi, i've havent explored everything available through the ApplicationBlock, but ill post my 2 cents worth.

    Yes you are right,  you still need to add the try... catch block around codes where there is a possible exception

     try
    {
    // DataAccess code that could throw an error
    }
    catch (Exception ex)
    {
       

    'you could use the method below to log the exception into eventviewer  

    ExceptionManager.Publish(ex);


    }

    add some configuration settings in your web config file.  

    <
    configSections>

    <section name="exceptionManagement" type="Microsoft.ApplicationBlocks.ExceptionManagement.ExceptionManagerSectionHandler,Microsoft.ApplicationBlocks.ExceptionManagement"/>

     </configSections>

    <exceptionManagement mode="on">

    <publisher assembly="Microsoft.ApplicationBlocks.ExceptionManagement" type="Microsoft.ApplicationBlocks.ExceptionManagement.DefaultPublisher" logname="logname" applicationname="applicationname"/>

    </exceptionManagement>

    You probably want to setup your own class to handle exceptions.  Use this class to handle exceptions caught in the try catch.... block. if you wanted to change the way you handle exceptions later on you only needed to change it in one place.

    public sealed class ExceptionManagement

    {

    public static void HandleException(Exception ex)

    {

    if (ex != null)

    {

    //logs the excetion. This will log it in the eventviewer

    ExceptionManager.Publish(ex); //if you dont like logging it in the eventviewer. write your own method to log it into the database/file.

    }

    }

    } 

     hope this helps cheers.

     

     

     

    Monday, September 10, 2007 1:28 AM