From 2cac5cafcecfcb0729891cbae19f5c54786db30f Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Thu, 27 Feb 2025 23:54:10 +0300 Subject: [PATCH 1/5] Make internal console writer more flexible via taking TextWriter only --- .../src/webdriver/Internal/Logging/LogContextManager.cs | 7 ++++--- .../{ConsoleLogHandler.cs => TextWriterHandler.cs} | 8 ++++---- dotnet/test/common/Internal/Logging/LogTest.cs | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) rename dotnet/src/webdriver/Internal/Logging/{ConsoleLogHandler.cs => TextWriterHandler.cs} (83%) diff --git a/dotnet/src/webdriver/Internal/Logging/LogContextManager.cs b/dotnet/src/webdriver/Internal/Logging/LogContextManager.cs index 834e806d8b74a..5e4b0097514a6 100644 --- a/dotnet/src/webdriver/Internal/Logging/LogContextManager.cs +++ b/dotnet/src/webdriver/Internal/Logging/LogContextManager.cs @@ -17,6 +17,7 @@ // under the License. // +using System; using System.Diagnostics.CodeAnalysis; using System.Threading; @@ -26,13 +27,13 @@ namespace OpenQA.Selenium.Internal.Logging { internal class LogContextManager { - private readonly AsyncLocal _currentAmbientLogContext = new AsyncLocal(); + private readonly AsyncLocal _currentAmbientLogContext = new(); public LogContextManager() { - var defaulConsoleLogHandler = new ConsoleLogHandler(); + var defaulConsoleLogHandler = new TextWriterHandler(Console.Error); - GlobalContext = new LogContext(LogEventLevel.Info, null, null, new[] { defaulConsoleLogHandler }); + GlobalContext = new LogContext(LogEventLevel.Info, null, null, [defaulConsoleLogHandler]); } public ILogContext GlobalContext { get; } diff --git a/dotnet/src/webdriver/Internal/Logging/ConsoleLogHandler.cs b/dotnet/src/webdriver/Internal/Logging/TextWriterHandler.cs similarity index 83% rename from dotnet/src/webdriver/Internal/Logging/ConsoleLogHandler.cs rename to dotnet/src/webdriver/Internal/Logging/TextWriterHandler.cs index 30a1a36efe128..9aa8545819c95 100644 --- a/dotnet/src/webdriver/Internal/Logging/ConsoleLogHandler.cs +++ b/dotnet/src/webdriver/Internal/Logging/TextWriterHandler.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information @@ -17,7 +17,7 @@ // under the License. // -using System; +using System.IO; #nullable enable @@ -26,7 +26,7 @@ namespace OpenQA.Selenium.Internal.Logging /// /// Represents a log handler that writes log events to the console. /// - public class ConsoleLogHandler : ILogHandler + public class TextWriterHandler(TextWriter writer) : ILogHandler { // performance trick to avoid expensive Enum.ToString() with fixed length private static readonly string[] _levels = { "TRACE", "DEBUG", " INFO", " WARN", "ERROR" }; @@ -37,7 +37,7 @@ public class ConsoleLogHandler : ILogHandler /// The log event to handle. public void Handle(LogEvent logEvent) { - Console.Error.WriteLine($"{logEvent.Timestamp:HH:mm:ss.fff} {_levels[(int)logEvent.Level]} {logEvent.IssuedBy.Name}: {logEvent.Message}"); + writer.WriteLine($"{logEvent.Timestamp:HH:mm:ss.fff} {_levels[(int)logEvent.Level]} {logEvent.IssuedBy.Name}: {logEvent.Message}"); } } } diff --git a/dotnet/test/common/Internal/Logging/LogTest.cs b/dotnet/test/common/Internal/Logging/LogTest.cs index a7d67684c2609..6a3a1f0b74781 100644 --- a/dotnet/test/common/Internal/Logging/LogTest.cs +++ b/dotnet/test/common/Internal/Logging/LogTest.cs @@ -31,7 +31,7 @@ public class LogTest private void ResetGlobalLog() { Log.SetLevel(LogEventLevel.Info); - Log.Handlers.Clear().Handlers.Add(new ConsoleLogHandler()); + Log.Handlers.Clear().Handlers.Add(new TextWriterHandler(Console.Error)); } [SetUp] From 2397729e9229faba89b48dbb2027c2427c4944d2 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Fri, 28 Feb 2025 00:00:46 +0300 Subject: [PATCH 2/5] Adjust xml docs --- dotnet/src/webdriver/Internal/Logging/TextWriterHandler.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/Internal/Logging/TextWriterHandler.cs b/dotnet/src/webdriver/Internal/Logging/TextWriterHandler.cs index 9aa8545819c95..ff21791d20d9e 100644 --- a/dotnet/src/webdriver/Internal/Logging/TextWriterHandler.cs +++ b/dotnet/src/webdriver/Internal/Logging/TextWriterHandler.cs @@ -24,7 +24,7 @@ namespace OpenQA.Selenium.Internal.Logging { /// - /// Represents a log handler that writes log events to the console. + /// Represents a log handler that writes log events to the given text writer. /// public class TextWriterHandler(TextWriter writer) : ILogHandler { @@ -32,7 +32,7 @@ public class TextWriterHandler(TextWriter writer) : ILogHandler private static readonly string[] _levels = { "TRACE", "DEBUG", " INFO", " WARN", "ERROR" }; /// - /// Handles a log event by writing it to the console. + /// Handles a log event by writing it to the text writer. /// /// The log event to handle. public void Handle(LogEvent logEvent) From b3ed53da4bc2cfc012bd19cabded671a3cf9baac Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Fri, 28 Feb 2025 00:16:52 +0300 Subject: [PATCH 3/5] Return back Console handler with deprecation message --- .../Internal/Logging/ConsoleHandler.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 dotnet/src/webdriver/Internal/Logging/ConsoleHandler.cs diff --git a/dotnet/src/webdriver/Internal/Logging/ConsoleHandler.cs b/dotnet/src/webdriver/Internal/Logging/ConsoleHandler.cs new file mode 100644 index 0000000000000..07e3176f493d4 --- /dev/null +++ b/dotnet/src/webdriver/Internal/Logging/ConsoleHandler.cs @@ -0,0 +1,31 @@ +// +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +using System; + +#nullable enable + +namespace OpenQA.Selenium.Internal.Logging +{ + [Obsolete("Use TextWriterHandler instead, will be removed in v4.32")] + /// + /// Represents a log handler that writes log events to the given text writer. + /// + public class ConsoleHandler() : TextWriterHandler(Console.Error); +} From 811acf6b49ad0fa0599f3856af6bf57ddfc12784 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Fri, 28 Feb 2025 00:19:17 +0300 Subject: [PATCH 4/5] ConsoleLogHandler is correct name --- .../Logging/{ConsoleHandler.cs => ConsoleLogHandler.cs} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename dotnet/src/webdriver/Internal/Logging/{ConsoleHandler.cs => ConsoleLogHandler.cs} (88%) diff --git a/dotnet/src/webdriver/Internal/Logging/ConsoleHandler.cs b/dotnet/src/webdriver/Internal/Logging/ConsoleLogHandler.cs similarity index 88% rename from dotnet/src/webdriver/Internal/Logging/ConsoleHandler.cs rename to dotnet/src/webdriver/Internal/Logging/ConsoleLogHandler.cs index 07e3176f493d4..16a66bce5aabd 100644 --- a/dotnet/src/webdriver/Internal/Logging/ConsoleHandler.cs +++ b/dotnet/src/webdriver/Internal/Logging/ConsoleLogHandler.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information @@ -27,5 +27,5 @@ namespace OpenQA.Selenium.Internal.Logging /// /// Represents a log handler that writes log events to the given text writer. /// - public class ConsoleHandler() : TextWriterHandler(Console.Error); + public class ConsoleLogHandler() : TextWriterHandler(Console.Error); } From 674f29c48b1f08e6c831dd0f31b21722bad0372f Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Fri, 28 Feb 2025 00:24:10 +0300 Subject: [PATCH 5/5] Adjust local variable name --- dotnet/src/webdriver/Internal/Logging/LogContextManager.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/Internal/Logging/LogContextManager.cs b/dotnet/src/webdriver/Internal/Logging/LogContextManager.cs index 5e4b0097514a6..d2eeca1355523 100644 --- a/dotnet/src/webdriver/Internal/Logging/LogContextManager.cs +++ b/dotnet/src/webdriver/Internal/Logging/LogContextManager.cs @@ -31,9 +31,9 @@ internal class LogContextManager public LogContextManager() { - var defaulConsoleLogHandler = new TextWriterHandler(Console.Error); + var defaulLogHandler = new TextWriterHandler(Console.Error); - GlobalContext = new LogContext(LogEventLevel.Info, null, null, [defaulConsoleLogHandler]); + GlobalContext = new LogContext(LogEventLevel.Info, null, null, [defaulLogHandler]); } public ILogContext GlobalContext { get; }