Suleman's Blog

Configuring Enterprise Library 4.0 for Exception handling and Logging

  • Comments 6

A few weeks back, I was trying to configure Enterprise Library 4.0 for Exception handling and Logging exception into event log but was not able to find any help on internet so it took me long time to do all the configurations for handling and logging exceptions using Enterprise. Library 4.0.

 

Follow following steps to configure Ent. Lib 4.0 Exception Handling and Logging blocks.

Configurations:

  1. Download and install Enterprise library 4.0, you can download it from here.
  2. After installation go to Start -> All Programs -> Microsoft patterns & practices -> Enterprise Library 4.0 – May 2008 -> Enterprise Library configuration
    clip_image002
  3. A new wizard console will open then go to File ->  Open Application -> (Navigate to your SharePoint web Application web.config) and click Open button.
    clip_image004
  4. After loading web.config, right click on the web.config path (see screen shot) –> New -> Exception Handling Application Block
    clip_image006
  5. A new block Exception Handling Application Block will be added to left pan of window.
  6. Right click on Exception Handling Application Block –> New –> Exception Policy
    clip_image008
  7. A new sub block will be added click on Exception Policy and type some meaningful name for your project in right pan of window
    image   
    Note: Exception Policy will change to new name you had written in this step in left pan as well. This step is the most important step, if multiple developers are working on the project you need to make sure everyone is using same name across the board otherwise you can run into issue while integration.
  8. Right click on My Custom Exception Policy(left pan) –> New -> Exception Type
    clip_image012
  9. A new dialog window will open look for Exception and click OK button.
    clip_image014
  10. A new sub block Exception will be added to left pan.
  11. Right click on Exception –> New –> Logging Handler
    clip_image016
  12. Another sub block Logging Handler will be added as well as a Logging Application Block. So left pan will looks like:
    clip_image018
  13. Click on Logging Handler and make following changes in Properties Pan.
  14. Click on Formatter Type -> click button (in value field)
    clip_image020
  15. A dialog window will open select TextExcetionFormatter then click OK
    clip_image022
  16. Select General in for LogCategory field.
    clip_image024
  17. Click on Formatted EvenLog TraceListener and change source to something more meaningful e.g. Application ABC Exception
    image 
    Note: This step is not important but it would be good if team uses same Source name across the board.
  18. Go to File -> Save Application to save all the configurations.

Web.Config Configurations:

Note: web.config changes are required if you are using Enterprise Library with SharePoint for .Net application you can skip these steps.

  1. Copy following DLLs from “C:\Program Files\Microsoft Enterprise Library 4.0 - May 2008\Bin” and paste them into your web App Bin directory or deploy into GAC.

    a. Microsoft.Practices.EnterpriseLibrary.Common.dll
    b. Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll
    c. Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.dll
    d. Microsoft.Practices.EnterpriseLibrary.Logging.dll
    e. Microsoft.Practices.ObjectBuilder2.dll
    f. Microsoft.Practices.Unity.dll

  2. Add following safe controls in web.config

    a. <SafeControl Assembly="Microsoft.Practices.EnterpriseLibrary.Common, Version=4.0.0.0, Culture=neutral,  PublicKeyToken=31bf3856ad364e35" Namespace="Microsoft.Practices.EnterpriseLibrary.Common" TypeName="*" Safe="True" />
    b.  <SafeControl Assembly="Microsoft.Practices.ObjectBuilder2, Version=2.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="Microsoft.Practices.ObjectBuilder2" TypeName="*" Safe="True" />
    c.  <SafeControl Assembly="Microsoft.Practices.Unity, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="Microsoft.Practices.Unity" TypeName="*" Safe="True" />
    d. <SafeControl Assembly="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" TypeName="*" Safe="True" />
    e.  <SafeControl Assembly="Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="Microsoft.Practices.EnterpriseLibrary.Logging" TypeName="*" Safe="True" />
    f. <SafeControl Assembly="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging" TypeName="*" Safe="True" />

Code:

Add following references and namespaces in your project

  1. Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
  2. Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging

Note: Use Browse tab while adding reference and add above references from “<System Drive>:\Program Files\Microsoft Enterprise Library 4.0 - May 2008\Bin”

User following code snippet for exception handling:

           try

            {

//CODE   

            }

            catch (Exception ex)

            {

                bool rethrow = ExceptionPolicy.HandleException(ex, "My Custom Exception Policy");

                // Policy Name should be same as we configured in configuration step 7

                if (rethrow)

                    throw; //Throw Exception if you need to show an error message to end user otherwise no need to throw exception.

            }

            finally

            {

// Close/Dispose all objects

            }

You are all done to get benefits of Enterprise Library 4.0 – Enjoy!!!!!

Leave a Comment
  • Hey Thanks for your input... i have followed above steps... looks i am getting follwing error.

    The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl, custompolicyname]) failed: Object reference not set to an instance of an object. (Strategy type ConfiguredObjectStrategy, index 2)

  • Make sure you are using same Exception Policy Name in following line of code as you set in Configuration Step 7

    bool rethrow = ExceptionPolicy.HandleException(ex, "My Custom Exception Policy");

  • I am having the most terrible time getting MS Entity Lib logging to work.  It works in our dev and local environments but not in production or staging.  I read an article about how you have to create sources programatically to so that you can write to the event log.  i tried that in one environment and it seemed to work for a few hours but now i am back to where i was.  I used the Entity Lib configuration tool to create a simple log app which also worked but now does not.  I feel like this whole set up should be a lot simpler and microsoft should have supplied much better doucmentation.  If you have any suggestions I would much appreciate it.  I am about ready to move on to some other tool.

  • where to add safecontrol assembly lines as shown in your code above in web.config file?

  • That code only requires if you are developing an application for SharePoint. If so then your application’s web.config should already have SafeControls tag in it. If not then following should be hierarchy in the web.config...

    <configuration>

       <SharePoint>

           <SafeControls>

                  <!-- above SafeControl lines should go here -->

           </SafeControls>

  • Hi,

    Thanks for your info.

    I would like to write a custom error component in SharePoint using Enterprise Library 4.1 (both exception handling & logging application block).

    Do you have any points to that? Or any resources on how to implement the above,

    Thanks

    Chandrasekaran.