Skip to content

Commit

Permalink
[dotnet] Lazy initialization of log handlers when required
Browse files Browse the repository at this point in the history
  • Loading branch information
nvborisenko committed Dec 18, 2023
1 parent 00b579d commit 3497094
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions dotnet/src/webdriver/Internal/Logging/LogContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ internal class LogContext : ILogContext

private readonly ILogContext _parentLogContext;

private readonly Lazy<LogHandlerList> _lazyLogHandlerList;

public LogContext(LogEventLevel level, ILogContext parentLogContext, ConcurrentDictionary<Type, ILogger> loggers, IEnumerable<ILogHandler> handlers)
{
_level = level;
Expand All @@ -45,7 +47,11 @@ public LogContext(LogEventLevel level, ILogContext parentLogContext, ConcurrentD

if (handlers is not null)
{
Handlers = new LogHandlerList(this, handlers);
_lazyLogHandlerList = new Lazy<LogHandlerList>(() => new LogHandlerList(this, handlers));
}
else
{
_lazyLogHandlerList = new Lazy<LogHandlerList>(() => new LogHandlerList(this));
}
}

Expand Down Expand Up @@ -125,7 +131,7 @@ public ILogContext SetLevel(Type issuer, LogEventLevel level)
return this;
}

public ILogHandlerList Handlers { get; }
public ILogHandlerList Handlers => _lazyLogHandlerList.Value;

public void Dispose()
{
Expand Down
8 changes: 8 additions & 0 deletions dotnet/test/common/Internal/Logging/LogTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ public void ShouldCreateContextWithCustomLevel()
Assert.That(logger.Level, Is.EqualTo(LogEventLevel.Warn));
}

[Test]
public void ShouldCreateContextWithNullLogHandlers()
{
var context = new LogContext(LogEventLevel.Info, null, null, handlers: null);

Assert.That(context.Handlers, Is.Empty);
}

[Test]
public void ContextShouldChangeLevel()
{
Expand Down

0 comments on commit 3497094

Please sign in to comment.