evicertia / Common.Logging.Extras

A collection of extra features and extensions for common.logging
Apache License 2.0
0 stars 2 forks source link

Duplicate key when pushing ThreadScopeVariables #5

Open IkerCelorrio opened 1 year ago

IkerCelorrio commented 1 year ago

When using PushThreadScopedVariablesFor on certain circunstances throw an ArgumentException indicating thate the key already has been added.

System.ArgumentException
  HResult=0x80070057
  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 Common.Logging.Scopes.ThreadLoggingScope.Set(String key, Object value)
   at Common.Logging.CommonLoggingExtensions.PushThreadScopedVariablesFor(ILog this, IEnumerable`1 variables, String prefix)
   at Herma.SmsDaemon.Startup.RebusFacility.OnBeforeMessage(IBus bus, Object message) in D:\repos\herma.smsdaemon\Herma.SmsDaemon\Startup\RebusFacility.cs:line 109
   at Rebus.BeforeMessageEventHandler.Invoke(IBus bus, Object message)
   at Rebus.Bus.RebusEvents.RaiseBeforeMessage(IBus bus, Object message)
   at Rebus.Bus.RebusBus.RaiseBeforeMessage(Object message)
   at System.Action`1.Invoke(T obj)
   at Rebus.Bus.Worker.<DoTry>d__2b.MoveNext()
IkerCelorrio commented 1 year ago

Issue caused by thread reuse in heavy task/continuationtask usage, encountered when using dotnetty and LogicalContext(LogicalThreadVariablesContext). The error is not reproduced when using Non-LogicalContext (PerThreadVariablesContext), so can be temporary workaround by setting FavorLogicalVariableContexts to false.