Need help with exception replacing/logging

Topics: Exception Handling Application Block, Logging Application Block
Apr 28, 2014 at 7:21 PM
Hey I'm new to the Ent. Lib and am struggling to get something to work. I am trying to create a wrapper class for entlib within my org to be used for our error handling. Rather than have the developers call the MS code directly i am trying to wrap the logic up in a class. However I'm struggling to make this work. Essentially there will be 2 policies; general and critical. A general error will be logged to db while the critical would replace, log, and email an admin.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Logging;

public class ExceptionHandler
    #region Private Variables

    private Exception _exception = null;
    private string _policy = null;
    private IConfigurationSource config = null;
    private ExceptionPolicyFactory policyFactory = null;
    private ExceptionManager exManager = null;


    #region Public Properties

    #region Constructors

    static ExceptionHandler()
        DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
        LogWriterFactory logWriterFactory = new LogWriterFactory();

    public ExceptionHandler(Exception ex, string policy)
        _exception = ex;
        _policy = policy;


    #region Private Methods

    private void Initialize()
        //Setup based upon app.config values

        config = ConfigurationSourceFactory.Create();
        policyFactory = new ExceptionPolicyFactory(config);
        exManager = policyFactory.CreateManager();


    #region Public Methods

    public void Log()

        //use ExceptionManager to handle logging and whatever other policies are set in the config.
        //exManager.HandleException(_exception, _policy);

        Exception newException;
        bool shouldRethrow = ExceptionPolicy.HandleException(_exception, _policy, out newException);
        if (shouldRethrow)
            if (newException != null) throw newException;

So basically in a try catch block would be like

catch(Exception ex){
ExceptionHandler handler = new ExceptionHandler(ex, "Critical")
So I am clearly doing something wrong here.. Any help would be appreciated, seriously
Apr 28, 2014 at 8:01 PM
Sorry I left one important part out... I can get the code to log and email if i am using a policy which just logs and emails, however.... I need to figure out a way to work with replacing messages in try/catch.. I seriously would appreciate any advice on the topic. Regards
Apr 29, 2014 at 5:33 AM
I'm not sure I follow. Can you expand on the specific issue you are having and what you are trying to achieve? Is it just the replace handler that is not working?

Randy Levy
Enterprise Library support engineer
Support How-to