Skip to content

Commit 2cac5ca

Browse files
committed
Make internal console writer more flexible via taking TextWriter only
1 parent 94007ce commit 2cac5ca

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using System;
2021
using System.Diagnostics.CodeAnalysis;
2122
using System.Threading;
2223

@@ -26,13 +27,13 @@ namespace OpenQA.Selenium.Internal.Logging
2627
{
2728
internal class LogContextManager
2829
{
29-
private readonly AsyncLocal<ILogContext?> _currentAmbientLogContext = new AsyncLocal<ILogContext?>();
30+
private readonly AsyncLocal<ILogContext?> _currentAmbientLogContext = new();
3031

3132
public LogContextManager()
3233
{
33-
var defaulConsoleLogHandler = new ConsoleLogHandler();
34+
var defaulConsoleLogHandler = new TextWriterHandler(Console.Error);
3435

35-
GlobalContext = new LogContext(LogEventLevel.Info, null, null, new[] { defaulConsoleLogHandler });
36+
GlobalContext = new LogContext(LogEventLevel.Info, null, null, [defaulConsoleLogHandler]);
3637
}
3738

3839
public ILogContext GlobalContext { get; }

dotnet/src/webdriver/Internal/Logging/ConsoleLogHandler.cs renamed to dotnet/src/webdriver/Internal/Logging/TextWriterHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <copyright file="ConsoleLogHandler.cs" company="Selenium Committers">
1+
// <copyright file="TextWriterHandler.cs" company="Selenium Committers">
22
// Licensed to the Software Freedom Conservancy (SFC) under one
33
// or more contributor license agreements. See the NOTICE file
44
// distributed with this work for additional information
@@ -17,7 +17,7 @@
1717
// under the License.
1818
// </copyright>
1919

20-
using System;
20+
using System.IO;
2121

2222
#nullable enable
2323

@@ -26,7 +26,7 @@ namespace OpenQA.Selenium.Internal.Logging
2626
/// <summary>
2727
/// Represents a log handler that writes log events to the console.
2828
/// </summary>
29-
public class ConsoleLogHandler : ILogHandler
29+
public class TextWriterHandler(TextWriter writer) : ILogHandler
3030
{
3131
// performance trick to avoid expensive Enum.ToString() with fixed length
3232
private static readonly string[] _levels = { "TRACE", "DEBUG", " INFO", " WARN", "ERROR" };
@@ -37,7 +37,7 @@ public class ConsoleLogHandler : ILogHandler
3737
/// <param name="logEvent">The log event to handle.</param>
3838
public void Handle(LogEvent logEvent)
3939
{
40-
Console.Error.WriteLine($"{logEvent.Timestamp:HH:mm:ss.fff} {_levels[(int)logEvent.Level]} {logEvent.IssuedBy.Name}: {logEvent.Message}");
40+
writer.WriteLine($"{logEvent.Timestamp:HH:mm:ss.fff} {_levels[(int)logEvent.Level]} {logEvent.IssuedBy.Name}: {logEvent.Message}");
4141
}
4242
}
4343
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class LogTest
3131
private void ResetGlobalLog()
3232
{
3333
Log.SetLevel(LogEventLevel.Info);
34-
Log.Handlers.Clear().Handlers.Add(new ConsoleLogHandler());
34+
Log.Handlers.Clear().Handlers.Add(new TextWriterHandler(Console.Error));
3535
}
3636

3737
[SetUp]

0 commit comments

Comments
 (0)