Cannot access a disposed object

Topics: Logging Application Block
Sep 24, 2015 at 4:46 PM
Hello,
I'm using the Loggin Application Block and often it happens that he application raise an exception with hat message :
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyEntry.ExecuteHandlerChain(Exception ex, Guid handlingInstanceID)
at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyEntry.Handle(Exception exceptionToHandle)
at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManager.HandleException(Exception exceptionToHandle, String policyName)
at Comdata.Log.ExceptionHelper.ExceptionHandle(Exception e, String category) in c:\DATA\Projects\GED\API\Common\Source_4.5\Comdata\Log\ExceptionHelper.cs:line 44
at Comdata.GED.Logistic.CustomerUI.Controllers.ClaimController.InsertTicket(Int32 typeID, String note, String dataRif) in c:\DATA\Projects\GED\API\Logistic\Source_4.5\Comdata.GED.Logistic.CustomerUI\Controllers\ClaimController.cs:line 315
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)

The application stays hang until an App Pool recycle occur.
Does someone can give me support ?

Following the library I created as wrapper :
public static class ExceptionHelper
{
    static ExceptionHelper()
    {
        LogWriterWrapper.Instance.Init();

        var factory = new ExceptionPolicyFactory();
        ExceptionPolicy.SetExceptionManager(factory.CreateManager(), false);
    }

    //the default category
    private const string CATEGORY = "BusinessPolicy";

    /// <summary>
    /// This method uses the default category (BusinessPolicy)
    /// </summary>
    /// <param name="e">The exception to handle</param>
    public static void ExceptionHandle(Exception e)
    {
        LogWriterWrapper.Instance.ReInit();
        ExceptionPolicy.HandleException(e, CATEGORY);
    }

    /// <summary>
    /// Use this overload in order to use a specific category to the handler
    /// </summary>
    /// <param name="e">The exception to handle</param>
    /// <param name="category">The name of the category to be used</param>
    public static void ExceptionHandle(Exception e, string category)
    {
        LogWriterWrapper.Instance.ReInit();
        ExceptionPolicy.HandleException(e, category);
    }
}
public class LogWriterWrapper
{
    private static readonly LogWriterWrapper instance = new LogWriterWrapper(); //static initializers are thread-safe
    public static LogWriterWrapper Instance { get { return instance; } }

    private LogWriterWrapper()
    {//code executing only once
        //todo - remove this line after understanding the error about logwriter
        //File.AppendAllText(Path.Combine(Path.GetDirectoryName(Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path)), "_Alt_Log.txt"), string.Format("{0} - LogWriterWrapper - singleton initialized", DateTime.Now.ToString("O")));
        var factoryLog = new LogWriterFactory();
        Logger.SetLogWriter(factoryLog.Create(), false);
    }

    public void Init()
    {

    }

    public void ReInit()
    {
        if (Logger.Writer == null)
        {
            //todo - remove this line after understanding the error about logwriter
            //File.AppendAllText(Path.Combine(Path.GetDirectoryName(Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path)), "_Alt_Log.txt"), string.Format("{0} - Logger.Writer is null - called from ReInit", DateTime.Now.ToString("O")));
            var factoryLog = new LogWriterFactory();
            Logger.SetLogWriter(factoryLog.Create(), false);
        }
    }
}
public static class LogHelper
{
    static LogHelper()
    {
        LogWriterWrapper.Instance.Init();
    }

    private const string DEBUG_CATEGORY = "Trace";
    private const string EVENT_CATEGORY = "General";

    public static void WriteEvent(string message, TraceEventType severity)
    {
        WriteEvent(message, 0, severity, EVENT_CATEGORY, string.Empty, DateTime.Now);
    }

    public static void WriteEvent(string message, int eventId, TraceEventType severity, string source, string title, DateTime activityDate)
    {
        LogWriterWrapper.Instance.ReInit();
        var le = new LogEntry();
        le.Categories.Add(source);
        le.Message = message;
        le.Severity = severity;
        le.EventId = eventId;
        le.Title = title;
        le.TimeStamp = activityDate;
        Logger.Write(le);
    }

    public static string DebugEvent(string message, params object[] args)
    {
        LogWriterWrapper.Instance.ReInit();
        var extendedInfo = new Dictionary<string, object>();
        var provider = new ManagedSecurityContextInformationProvider();
        extendedInfo.Add("IdentityName: ", provider.IdentityName);
        var le = new LogEntry();
        le.Categories.Add(DEBUG_CATEGORY);
        le.Message = string.Format(message, args);
        le.Severity = TraceEventType.Verbose;
        le.ExtendedProperties = extendedInfo;
        Logger.Write(le);
        return le.Message;
    }
}