Skip to content

Commit

Permalink
[dotnet] Consider log handlers as null when not initiated
Browse files Browse the repository at this point in the history
  • Loading branch information
nvborisenko committed Dec 18, 2023
1 parent dc4c7e4 commit 00b579d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
17 changes: 6 additions & 11 deletions dotnet/src/webdriver/Internal/Logging/LogContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ internal class LogContext : ILogContext

private readonly ILogContext _parentLogContext;

public LogContext(LogEventLevel level, ILogContext parentLogContext, ConcurrentDictionary<Type, ILogger> loggers, ILogHandlerList handlers)
public LogContext(LogEventLevel level, ILogContext parentLogContext, ConcurrentDictionary<Type, ILogger> loggers, IEnumerable<ILogHandler> handlers)
{
_level = level;

_parentLogContext = parentLogContext;

_loggers = loggers;

Handlers = handlers ?? new LogHandlerList(this);
if (handlers is not null)
{
Handlers = new LogHandlerList(this, handlers);
}
}

public ILogContext CreateContext()
Expand All @@ -60,15 +63,7 @@ public ILogContext CreateContext(LogEventLevel minimumLevel)
loggers = new ConcurrentDictionary<Type, ILogger>(_loggers.Select(l => new KeyValuePair<Type, ILogger>(l.Key, new Logger(l.Value.Issuer, minimumLevel))));
}

var context = new LogContext(minimumLevel, this, loggers, null);

if (Handlers != null)
{
foreach (var handler in Handlers)
{
context.Handlers.Add(handler);
}
}
var context = new LogContext(minimumLevel, this, loggers, Handlers);

Log.CurrentContext = context;

Expand Down
4 changes: 2 additions & 2 deletions dotnet/src/webdriver/Internal/Logging/LogContextManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ internal class LogContextManager

public LogContextManager()
{
_globalLogContext = new LogContext(LogEventLevel.Info, null, null, null);
var defaulConsoleLogHandler = new ConsoleLogHandler();

_globalLogContext.Handlers.Add(new ConsoleLogHandler());
_globalLogContext = new LogContext(LogEventLevel.Info, null, null, new[] { defaulConsoleLogHandler });
}

public ILogContext GlobalContext
Expand Down
7 changes: 7 additions & 0 deletions dotnet/src/webdriver/Internal/Logging/LogHandlerList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;

namespace OpenQA.Selenium.Internal.Logging
Expand All @@ -33,6 +34,12 @@ public LogHandlerList(ILogContext logContext)
_logContext = logContext;
}

public LogHandlerList(ILogContext logContext, IEnumerable<ILogHandler> handlers)
: base(handlers)
{
_logContext = logContext;
}

public new ILogContext Add(ILogHandler handler)
{
base.Add(handler);
Expand Down

0 comments on commit 00b579d

Please sign in to comment.