Skip to content

Commit 2159cd5

Browse files
nvborisenkosandeepsuryaprasad
authored andcommitted
[dotnet] Make internal console writer more flexible via taking TextWriter only (SeleniumHQ#15346)
1 parent 0fb447b commit 2159cd5

File tree

4 files changed

+51
-19
lines changed

4 files changed

+51
-19
lines changed

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,9 @@
2323

2424
namespace OpenQA.Selenium.Internal.Logging
2525
{
26+
[Obsolete("Use TextWriterHandler instead, will be removed in v4.32")]
2627
/// <summary>
27-
/// Represents a log handler that writes log events to the console.
28+
/// Represents a log handler that writes log events to the given text writer.
2829
/// </summary>
29-
public class ConsoleLogHandler : ILogHandler
30-
{
31-
// performance trick to avoid expensive Enum.ToString() with fixed length
32-
private static readonly string[] _levels = { "TRACE", "DEBUG", " INFO", " WARN", "ERROR" };
33-
34-
/// <summary>
35-
/// Handles a log event by writing it to the console.
36-
/// </summary>
37-
/// <param name="logEvent">The log event to handle.</param>
38-
public void Handle(LogEvent logEvent)
39-
{
40-
Console.Error.WriteLine($"{logEvent.Timestamp:HH:mm:ss.fff} {_levels[(int)logEvent.Level]} {logEvent.IssuedBy.Name}: {logEvent.Message}");
41-
}
42-
}
30+
public class ConsoleLogHandler() : TextWriterHandler(Console.Error);
4331
}

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 defaulLogHandler = new TextWriterHandler(Console.Error);
3435

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

3839
public ILogContext GlobalContext { get; }
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// <copyright file="TextWriterHandler.cs" company="Selenium Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
// </copyright>
19+
20+
using System.IO;
21+
22+
#nullable enable
23+
24+
namespace OpenQA.Selenium.Internal.Logging
25+
{
26+
/// <summary>
27+
/// Represents a log handler that writes log events to the given text writer.
28+
/// </summary>
29+
public class TextWriterHandler(TextWriter writer) : ILogHandler
30+
{
31+
// performance trick to avoid expensive Enum.ToString() with fixed length
32+
private static readonly string[] _levels = { "TRACE", "DEBUG", " INFO", " WARN", "ERROR" };
33+
34+
/// <summary>
35+
/// Handles a log event by writing it to the text writer.
36+
/// </summary>
37+
/// <param name="logEvent">The log event to handle.</param>
38+
public void Handle(LogEvent logEvent)
39+
{
40+
writer.WriteLine($"{logEvent.Timestamp:HH:mm:ss.fff} {_levels[(int)logEvent.Level]} {logEvent.IssuedBy.Name}: {logEvent.Message}");
41+
}
42+
}
43+
}

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)