Skip to content

Commit 3497094

Browse files
committed
[dotnet] Lazy initialization of log handlers when required
1 parent 00b579d commit 3497094

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ internal class LogContext : ILogContext
3535

3636
private readonly ILogContext _parentLogContext;
3737

38+
private readonly Lazy<LogHandlerList> _lazyLogHandlerList;
39+
3840
public LogContext(LogEventLevel level, ILogContext parentLogContext, ConcurrentDictionary<Type, ILogger> loggers, IEnumerable<ILogHandler> handlers)
3941
{
4042
_level = level;
@@ -45,7 +47,11 @@ public LogContext(LogEventLevel level, ILogContext parentLogContext, ConcurrentD
4547

4648
if (handlers is not null)
4749
{
48-
Handlers = new LogHandlerList(this, handlers);
50+
_lazyLogHandlerList = new Lazy<LogHandlerList>(() => new LogHandlerList(this, handlers));
51+
}
52+
else
53+
{
54+
_lazyLogHandlerList = new Lazy<LogHandlerList>(() => new LogHandlerList(this));
4955
}
5056
}
5157

@@ -125,7 +131,7 @@ public ILogContext SetLevel(Type issuer, LogEventLevel level)
125131
return this;
126132
}
127133

128-
public ILogHandlerList Handlers { get; }
134+
public ILogHandlerList Handlers => _lazyLogHandlerList.Value;
129135

130136
public void Dispose()
131137
{

dotnet/test/common/Internal/Logging/LogTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ public void ShouldCreateContextWithCustomLevel()
119119
Assert.That(logger.Level, Is.EqualTo(LogEventLevel.Warn));
120120
}
121121

122+
[Test]
123+
public void ShouldCreateContextWithNullLogHandlers()
124+
{
125+
var context = new LogContext(LogEventLevel.Info, null, null, handlers: null);
126+
127+
Assert.That(context.Handlers, Is.Empty);
128+
}
129+
122130
[Test]
123131
public void ContextShouldChangeLevel()
124132
{

0 commit comments

Comments
 (0)