EntLib 4 Exception: An item with the same key has already been added

Topics: Exception Handling Application Block, Logging Application Block
Jul 25, 2008 at 12:25 PM
It seems whenever I attempt to log exceptions to a RollingFileAppender with ExceptionPolicy.HandleException from timer events or from different threads, every so often, I get this "An item with the same key has already been added".

A side affect also observed is that the 1st exception generated in my app gets logged to a guid-prefixed log file and the main log file only gets the entry "System.UnhandledExceptionEventArgs"

Any ideas what's going on?

Here's the full exception.

Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException: The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl, LogException]) failed: The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter, null]) failed: An item with the same key has already been added. (Strategy type Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy, index 2) (Strategy type Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy, index 2) ---> Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl, LogException]) failed: The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter, null]) failed: An item with the same key has already been
 added. (Strategy type Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy, index 2) (Strategy type Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy, index 2) ---> Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter, null]) failed: An item with the same key has already been added. (Strategy type Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy, index 2) ---> System.ArgumentException: An item with the same key has already been added.
 at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
 at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
 at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
 at Microsoft.Practices.ObjectBuilder2.WeakRefDictionary`2.Add(TKey key, TValue value)
   at Microsoft.Practices.ObjectBuilder2.Locator.Add(Object key, Object value)
   at Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterCustomFactory.CreateObject(IBuilderContext context, String name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy.PreBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
   --- End of inner exception stack trace ---
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
   at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.Configuration.LoggingExceptionHandlerAssembler.Assemble(IBuilderContext context, ExceptionHandlerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.AssemblerBasedObjectFactory`2.Create(IBuilderContext context, TConfiguration objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
   at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyEntryCustomFactory.Create(IBuilderContext context, ExceptionTypeData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
   at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyCustomFactory.CreateObject(IBuilderContext context, String name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy.PreBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
   --- End of inner exception stack trace ---
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.Builder.BuildUp(IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList policies, IStrategyChain strategies, Object buildKey, Object existing)
   at Microsoft.Practices.ObjectBuilder2.Builder.BuildUp[TTypeToBuild](IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList policies, IStrategyChain strategies, Object buildKey, Object existing)
   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp[T](IReadWriteLocator locator, ILifetimeContainer lifetimeContainer, String id, IConfigurationSource configurationSource)
   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.LocatorNameTypeFactoryBase`1.Create(String name)
   at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.GetExceptionPolicy(Exception exception, String policyName, ExceptionPolicyFactory factory)
   --- End of inner exception stack trace ---
   at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.GetExceptionPolicy(Exception exception, String policyName, ExceptionPolicyFactory factory)
   at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(Exception exceptionToHandle, String policyName, ExceptionPolicyFactory policyFactory)
   at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(Exception exceptionToHandle, String policyName)
   at <my code...>
Jul 25, 2008 at 2:21 PM

Hi,

There is a configuration setting you can use on the logging exception handler's config to share the logging stack used by the Logger facade instead of creating a new one. This should take care of GUID prefix issue.

Regarding the exception you get, I'll log an issue.

Fernando

Jul 25, 2008 at 2:46 PM
Thanks for the followup. I'm unable to locate the config you mentioned in the docs. Is it LoggingExceptionHandlerData::UseDefaultLogger? Could you please post the setting name?

Thanks
Jul 25, 2008 at 2:53 PM
Yes, that's the setting.

Fernando
Aug 18, 2008 at 2:42 PM
I get the same exception with a Flat File Trace Listener, when an exception is logged inside a timer thread:

Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException was unhandled
  Message="The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl, ComServiceExceptions]) failed: The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter, null]) failed: An item with the same key has already been added. (Strategy type Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy, index 2) (Strategy type Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy, index 2)"
  Source="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
  StackTrace:
       at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.GetExceptionPolicy(Exception exception, String policyName, ExceptionPolicyFactory factory) in U:\dev\ref\EntLib4Src\Blocks\ExceptionHandling\Src\ExceptionHandling\ExceptionPolicy.cs:line 159
       at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(Exception exceptionToHandle, String policyName, ExceptionPolicyFactory policyFactory) in U:\dev\ref\EntLib4Src\Blocks\ExceptionHandling\Src\ExceptionHandling\ExceptionPolicy.cs:line 65
       at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(Exception exceptionToHandle, String policyName) in U:\dev\ref\EntLib4Src\Blocks\ExceptionHandling\Src\ExceptionHandling\ExceptionPolicy.cs:line 60
       at MyProject.ExceptionManager.HandleException(Exception ex, Object sender, TraceEventType eventType) in U:\dev\pro\MyProject\ExceptionManager.cs:line 44
       at MyProject.SessionManager.CommandTimerElapsed(Object state) in U:\dev\pro\MyProject\DeviceSessionManager.cs:line 222
       at System.Threading._TimerCallback.TimerCallback_Context(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading._TimerCallback.PerformTimerCallback(Object state)
  InnerException: Microsoft.Practices.ObjectBuilder2.BuildFailedException
       Message="The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl, ComServiceExceptions]) failed: The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter, null]) failed: An item with the same key has already been added. (Strategy type Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy, index 2) (Strategy type Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy, index 2)"
       Source="Microsoft.Practices.ObjectBuilder2"
       ExecutingStrategyIndex=2
       StackTrace:
            at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
            at Microsoft.Practices.ObjectBuilder2.Builder.BuildUp(IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList policies, IStrategyChain strategies, Object buildKey, Object existing)
            at Microsoft.Practices.ObjectBuilder2.Builder.BuildUp[TTypeToBuild](IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList policies, IStrategyChain strategies, Object buildKey, Object existing)
            at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp[T](IReadWriteLocator locator, ILifetimeContainer lifetimeContainer, String id, IConfigurationSource configurationSource) in U:\dev\ref\EntLib4Src\Blocks\Common\Src\Configuration\ObjectBuilder\EnterpriseLibraryFactory.cs:line 229
            at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.LocatorNameTypeFactoryBase`1.Create(String name) in U:\dev\ref\EntLib4Src\Blocks\Common\Src\Configuration\ObjectBuilder\LocatorNameTypeFactoryBase.cs:line 72
            at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.GetExceptionPolicy(Exception exception, String policyName, ExceptionPolicyFactory factory) in U:\dev\ref\EntLib4Src\Blocks\ExceptionHandling\Src\ExceptionHandling\ExceptionPolicy.cs:line 135
       InnerException: Microsoft.Practices.ObjectBuilder2.BuildFailedException
            Message="The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter, null]) failed: An item with the same key has already been added. (Strategy type Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy, index 2)"
            Source="Microsoft.Practices.ObjectBuilder2"
            ExecutingStrategyIndex=2
            StackTrace:
                 at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
                 at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.Configuration.LoggingExceptionHandlerAssembler.Assemble(IBuilderContext context, ExceptionHandlerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache) in U:\dev\ref\EntLib4Src\Blocks\ExceptionHandling\Src\Logging\Configuration\LoggingExceptionHandlerData.cs:line 285
                 at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.AssemblerBasedObjectFactory`2.Create(IBuilderContext context, TConfiguration objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache) in U:\dev\ref\EntLib4Src\Blocks\Common\Src\Configuration\ObjectBuilder\AssemblerBasedObjectFactory.cs:line 53
                 at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyEntryCustomFactory.Create(IBuilderContext context, ExceptionTypeData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache) in U:\dev\ref\EntLib4Src\Blocks\ExceptionHandling\Src\ExceptionHandling\ExceptionPolicyEntryCustomFactory.cs:line 47
                 at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyCustomFactory.CreateObject(IBuilderContext context, String name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache) in U:\dev\ref\EntLib4Src\Blocks\ExceptionHandling\Src\ExceptionHandling\ExceptionPolicyCustomFactory.cs:line 43
                 at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy.PreBuildUp(IBuilderContext context) in U:\dev\ref\EntLib4Src\Blocks\Common\Src\Configuration\ObjectBuilder\Extensions\ConfiguredObjectStrategy.cs:line 57
                 at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
            InnerException: System.ArgumentException
                 Message="An item with the same key has already been added."
                 Source="mscorlib"
                 StackTrace:
                      at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
                      at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
                      at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
                      at Microsoft.Practices.ObjectBuilder2.WeakRefDictionary`2.Add(TKey key, TValue value)
                      at Microsoft.Practices.ObjectBuilder2.Locator.Add(Object key, Object value)
                      at Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterCustomFactory.CreateObject(IBuilderContext context, String name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache) in U:\dev\ref\EntLib4Src\Blocks\Logging\Src\Logging\LogWriterCustomFactory.cs:line 55
                      at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy.PreBuildUp(IBuilderContext context) in U:\dev\ref\EntLib4Src\Blocks\Common\Src\Configuration\ObjectBuilder\Extensions\ConfiguredObjectStrategy.cs:line 57
                      at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)