Skip to content

Commit 6efac84

Browse files
committed
Move logic to clone loggers
1 parent df09141 commit 6efac84

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

dotnet/src/webdriver/Internal/Logging/LogContext.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System;
2121
using System.Collections.Concurrent;
2222
using System.Collections.Generic;
23+
using System.Diagnostics.CodeAnalysis;
2324
using 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

Comments
 (0)