2020using System ;
2121using System . Collections . Concurrent ;
2222using System . Collections . Generic ;
23+ using System . Diagnostics . CodeAnalysis ;
2324using System . Linq ;
2425
2526#nullable enable
@@ -46,10 +47,7 @@ public LogContext(LogEventLevel level, ILogContext? parentLogContext, Concurrent
4647
4748 _parentLogContext = parentLogContext ;
4849
49- if ( loggers is not null )
50- {
51- _loggers = new ConcurrentDictionary < Type , ILogger > ( loggers . Select ( l => new KeyValuePair < Type , ILogger > ( l . Key , new Logger ( l . Value . Issuer , level ) ) ) ) ;
52- }
50+ _loggers = CloneLoggers ( loggers , level ) ;
5351
5452 if ( handlers is not null )
5553 {
@@ -158,5 +156,24 @@ public void Dispose()
158156
159157 Log . CurrentContext = _parentLogContext ;
160158 }
159+
160+ [ return : NotNullIfNotNull ( nameof ( loggers ) ) ]
161+ private static ConcurrentDictionary < Type , ILogger > ? CloneLoggers ( ConcurrentDictionary < Type , ILogger > ? loggers , LogEventLevel minimumLevel )
162+ {
163+ if ( loggers is null )
164+ {
165+ return null ;
166+ }
167+
168+ var cloned = new Dictionary < Type , ILogger > ( loggers . Count ) ;
169+
170+ foreach ( KeyValuePair < Type , ILogger > logger in loggers )
171+ {
172+ var clonedLogger = new Logger ( logger . Value . Issuer , minimumLevel ) ;
173+ cloned . Add ( logger . Key , clonedLogger ) ;
174+ }
175+
176+ return new ConcurrentDictionary < Type , ILogger > ( cloned ) ;
177+ }
161178 }
162179}
0 commit comments