diff --git a/dotnet/src/webdriver/Internal/Logging/ConsoleLogHandler.cs b/dotnet/src/webdriver/Internal/Logging/ConsoleLogHandler.cs index 30a1a36efe128..16a66bce5aabd 100644 --- a/dotnet/src/webdriver/Internal/Logging/ConsoleLogHandler.cs +++ b/dotnet/src/webdriver/Internal/Logging/ConsoleLogHandler.cs @@ -23,21 +23,9 @@ 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 console. + /// Represents a log handler that writes log events to the given text writer. /// - public class ConsoleLogHandler : ILogHandler - { - // performance trick to avoid expensive Enum.ToString() with fixed length - private static readonly string[] _levels = { "TRACE", "DEBUG", " INFO", " WARN", "ERROR" }; - - /// - /// Handles a log event by writing it to the console. - /// - /// 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}"); - } - } + public class ConsoleLogHandler() : TextWriterHandler(Console.Error); } diff --git a/dotnet/src/webdriver/Internal/Logging/LogContextManager.cs b/dotnet/src/webdriver/Internal/Logging/LogContextManager.cs index 834e806d8b74a..d2eeca1355523 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 defaulLogHandler = new TextWriterHandler(Console.Error); - GlobalContext = new LogContext(LogEventLevel.Info, null, null, new[] { defaulConsoleLogHandler }); + GlobalContext = new LogContext(LogEventLevel.Info, null, null, [defaulLogHandler]); } public ILogContext GlobalContext { get; } diff --git a/dotnet/src/webdriver/Internal/Logging/TextWriterHandler.cs b/dotnet/src/webdriver/Internal/Logging/TextWriterHandler.cs new file mode 100644 index 0000000000000..ff21791d20d9e --- /dev/null +++ b/dotnet/src/webdriver/Internal/Logging/TextWriterHandler.cs @@ -0,0 +1,43 @@ +// +// 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.IO; + +#nullable enable + +namespace OpenQA.Selenium.Internal.Logging +{ + /// + /// Represents a log handler that writes log events to the given text writer. + /// + 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" }; + + /// + /// Handles a log event by writing it to the text writer. + /// + /// The log event to handle. + public void Handle(LogEvent logEvent) + { + 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]