From 7316d588e5df077a19f7ba6c25268279817abab8 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 30 Mar 2022 22:19:40 +0300 Subject: [PATCH 01/11] First pass --- .../Common/src/Extensions/Logging/NullScope.cs | 2 ++ .../src/Interop/Windows/Interop.Libraries.cs | 2 ++ .../Windows/Kernel32/Interop.GetConsoleMode.cs | 2 ++ .../Windows/Kernel32/Interop.GetStdHandle.cs | 2 ++ .../System/Text/Json/PooledByteBufferWriter.cs | 2 ++ ...icrosoft.Extensions.Logging.Abstractions.cs | 2 +- .../src/LogEntry.cs | 2 +- .../src/ConsoleFormatter.cs | 2 +- .../src/ConsoleFormatterOptions.cs | 2 +- .../src/ConsoleLogger.cs | 16 ++++++++-------- .../src/ConsoleLoggerOptions.cs | 4 ++-- .../src/ConsoleLoggerProcessor.cs | 11 +++++++---- .../src/ConsoleLoggerProvider.cs | 12 +++++++----- .../src/FormatterOptionsMonitor.cs | 4 ++-- .../src/JsonConsoleFormatter.cs | 14 ++++++++------ ...Microsoft.Extensions.Logging.Console.csproj | 4 ++-- .../src/SimpleConsoleFormatter.cs | 18 ++++++++++-------- .../src/SystemdConsoleFormatter.cs | 14 ++++++++------ 18 files changed, 68 insertions(+), 47 deletions(-) diff --git a/src/libraries/Common/src/Extensions/Logging/NullScope.cs b/src/libraries/Common/src/Extensions/Logging/NullScope.cs index 7748734de07be..83ba07d84b9b4 100644 --- a/src/libraries/Common/src/Extensions/Logging/NullScope.cs +++ b/src/libraries/Common/src/Extensions/Logging/NullScope.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#nullable enable + using System; namespace Microsoft.Extensions.Logging diff --git a/src/libraries/Common/src/Interop/Windows/Interop.Libraries.cs b/src/libraries/Common/src/Interop/Windows/Interop.Libraries.cs index 81b31db1cff99..55e0128f58340 100644 --- a/src/libraries/Common/src/Interop/Windows/Interop.Libraries.cs +++ b/src/libraries/Common/src/Interop/Windows/Interop.Libraries.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#nullable enable + internal static partial class Interop { internal static partial class Libraries diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetConsoleMode.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetConsoleMode.cs index 1b2ee78b33ae6..6b30aa5d5fff5 100644 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetConsoleMode.cs +++ b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetConsoleMode.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#nullable enable + using System; using System.Runtime.InteropServices; diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetStdHandle.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetStdHandle.cs index e763e01c82196..bf781b15407af 100644 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetStdHandle.cs +++ b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetStdHandle.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#nullable enable + using System; using System.Runtime.InteropServices; diff --git a/src/libraries/Common/src/System/Text/Json/PooledByteBufferWriter.cs b/src/libraries/Common/src/System/Text/Json/PooledByteBufferWriter.cs index 8694a785ad3a5..64923524da448 100644 --- a/src/libraries/Common/src/System/Text/Json/PooledByteBufferWriter.cs +++ b/src/libraries/Common/src/System/Text/Json/PooledByteBufferWriter.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#nullable enable + using System.Buffers; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/ref/Microsoft.Extensions.Logging.Abstractions.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/ref/Microsoft.Extensions.Logging.Abstractions.cs index ee48f8f22bb39..72e6ae6afce77 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/ref/Microsoft.Extensions.Logging.Abstractions.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/ref/Microsoft.Extensions.Logging.Abstractions.cs @@ -160,7 +160,7 @@ public readonly partial struct LogEntry public string Category { get { throw null; } } public Microsoft.Extensions.Logging.EventId EventId { get { throw null; } } public System.Exception? Exception { get { throw null; } } - public System.Func? Formatter { get { throw null; } } + public System.Func Formatter { get { throw null; } } public Microsoft.Extensions.Logging.LogLevel LogLevel { get { throw null; } } public TState State { get { throw null; } } } diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LogEntry.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LogEntry.cs index cebd054da91b7..17f070cf02391 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LogEntry.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LogEntry.cs @@ -57,6 +57,6 @@ public LogEntry(LogLevel logLevel, string category, EventId eventId, TState stat /// /// Gets the formatter /// - public Func? Formatter { get; } + public Func Formatter { get; } } } diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleFormatter.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleFormatter.cs index b4c20770f830c..0fbaaf80a8e1e 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleFormatter.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleFormatter.cs @@ -32,6 +32,6 @@ protected ConsoleFormatter(string name!!) /// The provider of scope data. /// The string writer embedding ansi code for colors. /// The type of the object to be written. - public abstract void Write(in LogEntry logEntry, IExternalScopeProvider scopeProvider, TextWriter textWriter); + public abstract void Write(in LogEntry logEntry, IExternalScopeProvider? scopeProvider, TextWriter textWriter); } } diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleFormatterOptions.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleFormatterOptions.cs index e4a9800ad6e3e..18978a2c40a61 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleFormatterOptions.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleFormatterOptions.cs @@ -23,7 +23,7 @@ public ConsoleFormatterOptions() { } /// Gets or sets format string used to format timestamp in logging messages. Defaults to null. /// [StringSyntax(StringSyntaxAttribute.DateTimeFormat)] - public string TimestampFormat { get; set; } + public string? TimestampFormat { get; set; } /// /// Gets or sets indication whether or not UTC timezone should be used to format timestamps in logging messages. Defaults to false. diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs index 301ed0a9fbc5f..5c29492da1894 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs @@ -20,15 +20,15 @@ internal ConsoleLogger(string name!!, ConsoleLoggerProcessor loggerProcessor) _queueProcessor = loggerProcessor; } - internal ConsoleFormatter Formatter { get; set; } - internal IExternalScopeProvider ScopeProvider { get; set; } + internal ConsoleFormatter? Formatter { get; set; } + internal IExternalScopeProvider? ScopeProvider { get; set; } - internal ConsoleLoggerOptions Options { get; set; } + internal ConsoleLoggerOptions? Options { get; set; } [ThreadStatic] - private static StringWriter t_stringWriter; + private static StringWriter? t_stringWriter; - public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) { if (!IsEnabled(logLevel)) { @@ -40,7 +40,7 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except } t_stringWriter ??= new StringWriter(); LogEntry logEntry = new LogEntry(logLevel, _name, eventId, state, exception, formatter); - Formatter.Write(in logEntry, ScopeProvider, t_stringWriter); + Formatter!.Write(in logEntry, ScopeProvider, t_stringWriter); var sb = t_stringWriter.GetStringBuilder(); if (sb.Length == 0) @@ -53,7 +53,7 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except { sb.Capacity = 1024; } - _queueProcessor.EnqueueMessage(new LogMessageEntry(computedAnsiString, logAsError: logLevel >= Options.LogToStandardErrorThreshold)); + _queueProcessor.EnqueueMessage(new LogMessageEntry(computedAnsiString, logAsError: logLevel >= Options!.LogToStandardErrorThreshold)); } public bool IsEnabled(LogLevel logLevel) @@ -61,6 +61,6 @@ public bool IsEnabled(LogLevel logLevel) return logLevel != LogLevel.None; } - public IDisposable BeginScope(TState state) => ScopeProvider?.Push(state) ?? NullScope.Instance; + public IDisposable BeginScope(TState state) where TState : notnull => ScopeProvider?.Push(state) ?? NullScope.Instance; } } diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerOptions.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerOptions.cs index 694d0b6c2da3a..4036fc258add3 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerOptions.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerOptions.cs @@ -39,7 +39,7 @@ public ConsoleLoggerFormat Format /// /// Name of the log message formatter to use. Defaults to "simple" />. /// - public string FormatterName { get; set; } + public string? FormatterName { get; set; } /// /// Includes scopes when . @@ -56,7 +56,7 @@ public ConsoleLoggerFormat Format /// Gets or sets format string used to format timestamp in logging messages. Defaults to null. /// [System.ObsoleteAttribute("ConsoleLoggerOptions.TimestampFormat has been deprecated. Use ConsoleFormatterOptions.TimestampFormat instead.")] - public string TimestampFormat { get; set; } + public string? TimestampFormat { get; set; } /// /// Gets or sets indication whether or not UTC timezone should be used to format timestamps in logging messages. Defaults to false. diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProcessor.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProcessor.cs index c79ccac72f9fd..53f68c4d96009 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProcessor.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProcessor.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Concurrent; +using System.Diagnostics.CodeAnalysis; using System.Runtime.Versioning; using System.Threading; @@ -16,8 +17,10 @@ internal class ConsoleLoggerProcessor : IDisposable private readonly BlockingCollection _messageQueue = new BlockingCollection(_maxQueuedMessages); private readonly Thread _outputThread; - public IConsole Console; - public IConsole ErrorConsole; + [DisallowNull] + public IConsole? Console; + [DisallowNull] + public IConsole? ErrorConsole; public ConsoleLoggerProcessor() { @@ -53,8 +56,8 @@ public virtual void EnqueueMessage(LogMessageEntry message) // for testing internal virtual void WriteMessage(LogMessageEntry entry) { - IConsole console = entry.LogAsError ? ErrorConsole : Console; - console.Write(entry.Message); + IConsole? console = entry.LogAsError ? ErrorConsole : Console; + console!.Write(entry.Message); } private void ProcessLogQueue() diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs index 8c71e96e21f9d..ff3b85eacad5d 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; using System.Runtime.Versioning; using Microsoft.Extensions.Options; @@ -22,7 +23,7 @@ public class ConsoleLoggerProvider : ILoggerProvider, ISupportExternalScope private ConcurrentDictionary _formatters; private readonly ConsoleLoggerProcessor _messageQueue; - private IDisposable _optionsReloadToken; + private IDisposable? _optionsReloadToken; private IExternalScopeProvider _scopeProvider = NullExternalScopeProvider.Instance; /// @@ -78,7 +79,8 @@ private static bool DoesConsoleSupportAnsi() return (consoleMode & Interop.Kernel32.ENABLE_VIRTUAL_TERMINAL_PROCESSING) == Interop.Kernel32.ENABLE_VIRTUAL_TERMINAL_PROCESSING; } - private void SetFormatters(IEnumerable formatters = null) + [MemberNotNull(nameof(_formatters))] + private void SetFormatters(IEnumerable? formatters = null) { var cd = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); @@ -105,7 +107,7 @@ private void SetFormatters(IEnumerable formatters = null) // warning: ReloadLoggerOptions can be called before the ctor completed,... before registering all of the state used in this method need to be initialized private void ReloadLoggerOptions(ConsoleLoggerOptions options) { - if (options.FormatterName == null || !_formatters.TryGetValue(options.FormatterName, out ConsoleFormatter logFormatter)) + if (options.FormatterName == null || !_formatters.TryGetValue(options.FormatterName, out ConsoleFormatter? logFormatter)) { #pragma warning disable CS0618 logFormatter = options.Format switch @@ -130,7 +132,7 @@ private void ReloadLoggerOptions(ConsoleLoggerOptions options) /// public ILogger CreateLogger(string name) { - if (_options.CurrentValue.FormatterName == null || !_formatters.TryGetValue(_options.CurrentValue.FormatterName, out ConsoleFormatter logFormatter)) + if (_options.CurrentValue.FormatterName == null || !_formatters.TryGetValue(_options.CurrentValue.FormatterName, out ConsoleFormatter? logFormatter)) { #pragma warning disable CS0618 logFormatter = _options.CurrentValue.Format switch @@ -146,7 +148,7 @@ public ILogger CreateLogger(string name) } } - return _loggers.TryGetValue(name, out ConsoleLogger logger) ? + return _loggers.TryGetValue(name, out ConsoleLogger? logger) ? logger : _loggers.GetOrAdd(name, new ConsoleLogger(name, _messageQueue) { diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/FormatterOptionsMonitor.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/FormatterOptionsMonitor.cs index 16f1d5572893c..a47d455b4f5f1 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/FormatterOptionsMonitor.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/FormatterOptionsMonitor.cs @@ -17,9 +17,9 @@ public FormatterOptionsMonitor(TOptions options) _options = options; } - public TOptions Get(string name) => _options; + public TOptions Get(string? name) => _options; - public IDisposable OnChange(Action listener) + public IDisposable? OnChange(Action listener) { return null; } diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs index f6bdea56c57bd..2e07ed89cc43d 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; using System.Runtime.InteropServices; @@ -15,16 +16,16 @@ namespace Microsoft.Extensions.Logging.Console { internal sealed class JsonConsoleFormatter : ConsoleFormatter, IDisposable { - private IDisposable _optionsReloadToken; + private IDisposable? _optionsReloadToken; public JsonConsoleFormatter(IOptionsMonitor options) - : base (ConsoleFormatterNames.Json) + : base(ConsoleFormatterNames.Json) { ReloadLoggerOptions(options.CurrentValue); _optionsReloadToken = options.OnChange(ReloadLoggerOptions); } - public override void Write(in LogEntry logEntry, IExternalScopeProvider scopeProvider, TextWriter textWriter) + public override void Write(in LogEntry logEntry, IExternalScopeProvider? scopeProvider, TextWriter textWriter) { string message = logEntry.Formatter(logEntry.State, logEntry.Exception); if (logEntry.Exception == null && message == null) @@ -34,7 +35,7 @@ public override void Write(in LogEntry logEntry, IExternalScopeP LogLevel logLevel = logEntry.LogLevel; string category = logEntry.Category; int eventId = logEntry.EventId.Id; - Exception exception = logEntry.Exception; + Exception? exception = logEntry.Exception; const int DefaultBufferSize = 1024; using (var output = new PooledByteBufferWriter(DefaultBufferSize)) { @@ -102,7 +103,7 @@ private static string GetLogLevelString(LogLevel logLevel) }; } - private void WriteScopeInformation(Utf8JsonWriter writer, IExternalScopeProvider scopeProvider) + private void WriteScopeInformation(Utf8JsonWriter writer, IExternalScopeProvider? scopeProvider) { if (FormatterOptions.IncludeScopes && scopeProvider != null) { @@ -185,10 +186,11 @@ private static void WriteItem(Utf8JsonWriter writer, KeyValuePair Convert.ToString(obj, CultureInfo.InvariantCulture); + private static string? ToInvariantString(object? obj) => Convert.ToString(obj, CultureInfo.InvariantCulture); internal JsonConsoleFormatterOptions FormatterOptions { get; set; } + [MemberNotNull(nameof(FormatterOptions))] private void ReloadLoggerOptions(JsonConsoleFormatterOptions options) { FormatterOptions = options; diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/Microsoft.Extensions.Logging.Console.csproj b/src/libraries/Microsoft.Extensions.Logging.Console/src/Microsoft.Extensions.Logging.Console.csproj index 873941da0e6ad..f337f78cc21a4 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/Microsoft.Extensions.Logging.Console.csproj +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/Microsoft.Extensions.Logging.Console.csproj @@ -1,9 +1,9 @@ - + $(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum) + enable true - annotations true $(DefineConstants);NO_SUPPRESS_GC_TRANSITION diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/SimpleConsoleFormatter.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/SimpleConsoleFormatter.cs index 0ec05de570060..7ca3f232e87dd 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/SimpleConsoleFormatter.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/SimpleConsoleFormatter.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Diagnostics.CodeAnalysis; using System.IO; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; @@ -13,15 +14,16 @@ internal sealed class SimpleConsoleFormatter : ConsoleFormatter, IDisposable private const string LoglevelPadding = ": "; private static readonly string _messagePadding = new string(' ', GetLogLevelString(LogLevel.Information).Length + LoglevelPadding.Length); private static readonly string _newLineWithMessagePadding = Environment.NewLine + _messagePadding; - private IDisposable _optionsReloadToken; + private IDisposable? _optionsReloadToken; public SimpleConsoleFormatter(IOptionsMonitor options) - : base (ConsoleFormatterNames.Simple) + : base(ConsoleFormatterNames.Simple) { ReloadLoggerOptions(options.CurrentValue); _optionsReloadToken = options.OnChange(ReloadLoggerOptions); } + [MemberNotNull(nameof(FormatterOptions))] private void ReloadLoggerOptions(SimpleConsoleFormatterOptions options) { FormatterOptions = options; @@ -34,7 +36,7 @@ public void Dispose() internal SimpleConsoleFormatterOptions FormatterOptions { get; set; } - public override void Write(in LogEntry logEntry, IExternalScopeProvider scopeProvider, TextWriter textWriter) + public override void Write(in LogEntry logEntry, IExternalScopeProvider? scopeProvider, TextWriter textWriter) { string message = logEntry.Formatter(logEntry.State, logEntry.Exception); if (logEntry.Exception == null && message == null) @@ -45,8 +47,8 @@ public override void Write(in LogEntry logEntry, IExternalScopeP ConsoleColors logLevelColors = GetLogLevelConsoleColors(logLevel); string logLevelString = GetLogLevelString(logLevel); - string timestamp = null; - string timestampFormat = FormatterOptions.TimestampFormat; + string? timestamp = null; + string? timestampFormat = FormatterOptions.TimestampFormat; if (timestampFormat != null) { DateTimeOffset dateTimeOffset = GetCurrentDateTime(); @@ -63,11 +65,11 @@ public override void Write(in LogEntry logEntry, IExternalScopeP CreateDefaultLogMessage(textWriter, logEntry, message, scopeProvider); } - private void CreateDefaultLogMessage(TextWriter textWriter, in LogEntry logEntry, string message, IExternalScopeProvider scopeProvider) + private void CreateDefaultLogMessage(TextWriter textWriter, in LogEntry logEntry, string message, IExternalScopeProvider? scopeProvider) { bool singleLine = FormatterOptions.SingleLine; int eventId = logEntry.EventId.Id; - Exception exception = logEntry.Exception; + Exception? exception = logEntry.Exception; // Example: // info: ConsoleApp.Program[10] @@ -175,7 +177,7 @@ private ConsoleColors GetLogLevelConsoleColors(LogLevel logLevel) }; } - private void WriteScopeInformation(TextWriter textWriter, IExternalScopeProvider scopeProvider, bool singleLine) + private void WriteScopeInformation(TextWriter textWriter, IExternalScopeProvider? scopeProvider, bool singleLine) { if (FormatterOptions.IncludeScopes && scopeProvider != null) { diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/SystemdConsoleFormatter.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/SystemdConsoleFormatter.cs index 7a3e6dabb925c..11fffdc304338 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/SystemdConsoleFormatter.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/SystemdConsoleFormatter.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Text; using Microsoft.Extensions.Logging.Abstractions; @@ -11,7 +12,7 @@ namespace Microsoft.Extensions.Logging.Console { internal sealed class SystemdConsoleFormatter : ConsoleFormatter, IDisposable { - private IDisposable _optionsReloadToken; + private IDisposable? _optionsReloadToken; public SystemdConsoleFormatter(IOptionsMonitor options) : base(ConsoleFormatterNames.Systemd) @@ -20,6 +21,7 @@ public SystemdConsoleFormatter(IOptionsMonitor options) _optionsReloadToken = options.OnChange(ReloadLoggerOptions); } + [MemberNotNull(nameof(FormatterOptions))] private void ReloadLoggerOptions(ConsoleFormatterOptions options) { FormatterOptions = options; @@ -32,7 +34,7 @@ public void Dispose() internal ConsoleFormatterOptions FormatterOptions { get; set; } - public override void Write(in LogEntry logEntry, IExternalScopeProvider scopeProvider, TextWriter textWriter) + public override void Write(in LogEntry logEntry, IExternalScopeProvider? scopeProvider, TextWriter textWriter) { string message = logEntry.Formatter(logEntry.State, logEntry.Exception); if (logEntry.Exception == null && message == null) @@ -42,8 +44,8 @@ public override void Write(in LogEntry logEntry, IExternalScopeP LogLevel logLevel = logEntry.LogLevel; string category = logEntry.Category; int eventId = logEntry.EventId.Id; - Exception exception = logEntry.Exception; - // systemd reads messages from standard out line-by-line in a 'message' format. + Exception? exception = logEntry.Exception; + // system reads messages from standard out line-by-line in a 'message' format. // newline characters are treated as message delimiters, so we must replace them. // Messages longer than the journal LineMax setting (default: 48KB) are cropped. // Example: @@ -54,7 +56,7 @@ public override void Write(in LogEntry logEntry, IExternalScopeP textWriter.Write(logLevelString); // timestamp - string timestampFormat = FormatterOptions.TimestampFormat; + string? timestampFormat = FormatterOptions.TimestampFormat; if (timestampFormat != null) { DateTimeOffset dateTimeOffset = GetCurrentDateTime(); @@ -116,7 +118,7 @@ private static string GetSyslogSeverityString(LogLevel logLevel) }; } - private void WriteScopeInformation(TextWriter textWriter, IExternalScopeProvider scopeProvider) + private void WriteScopeInformation(TextWriter textWriter, IExternalScopeProvider? scopeProvider) { if (FormatterOptions.IncludeScopes && scopeProvider != null) { From 1d73637b79a7b3deaecbb2a8cb1c04652f3f4a93 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 30 Mar 2022 22:28:54 +0300 Subject: [PATCH 02/11] ref --- .../ref/Microsoft.Extensions.Logging.Console.cs | 8 ++++---- .../ref/Microsoft.Extensions.Logging.Console.csproj | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/ref/Microsoft.Extensions.Logging.Console.cs b/src/libraries/Microsoft.Extensions.Logging.Console/ref/Microsoft.Extensions.Logging.Console.cs index c315783ab58c0..f882ecc5ecc8b 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/ref/Microsoft.Extensions.Logging.Console.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/ref/Microsoft.Extensions.Logging.Console.cs @@ -29,7 +29,7 @@ public abstract partial class ConsoleFormatter { protected ConsoleFormatter(string name) { } public string Name { get { throw null; } } - public abstract void Write(in Microsoft.Extensions.Logging.Abstractions.LogEntry logEntry, Microsoft.Extensions.Logging.IExternalScopeProvider scopeProvider, System.IO.TextWriter textWriter); + public abstract void Write(in Microsoft.Extensions.Logging.Abstractions.LogEntry logEntry, Microsoft.Extensions.Logging.IExternalScopeProvider? scopeProvider, System.IO.TextWriter textWriter); } public static partial class ConsoleFormatterNames { @@ -42,7 +42,7 @@ public partial class ConsoleFormatterOptions public ConsoleFormatterOptions() { } public bool IncludeScopes { get { throw null; } set { } } [System.Diagnostics.CodeAnalysis.StringSyntaxAttribute(System.Diagnostics.CodeAnalysis.StringSyntaxAttribute.DateTimeFormat)] - public string TimestampFormat { get { throw null; } set { } } + public string? TimestampFormat { get { throw null; } set { } } public bool UseUtcTimestamp { get { throw null; } set { } } } [System.ObsoleteAttribute("ConsoleLoggerFormat has been deprecated.")] @@ -58,12 +58,12 @@ public ConsoleLoggerOptions() { } public bool DisableColors { get { throw null; } set { } } [System.ObsoleteAttribute("ConsoleLoggerOptions.Format has been deprecated. Use ConsoleLoggerOptions.FormatterName instead.")] public Microsoft.Extensions.Logging.Console.ConsoleLoggerFormat Format { get { throw null; } set { } } - public string FormatterName { get { throw null; } set { } } + public string? FormatterName { get { throw null; } set { } } [System.ObsoleteAttribute("ConsoleLoggerOptions.IncludeScopes has been deprecated. Use ConsoleFormatterOptions.IncludeScopes instead.")] public bool IncludeScopes { get { throw null; } set { } } public Microsoft.Extensions.Logging.LogLevel LogToStandardErrorThreshold { get { throw null; } set { } } [System.ObsoleteAttribute("ConsoleLoggerOptions.TimestampFormat has been deprecated. Use ConsoleFormatterOptions.TimestampFormat instead.")] - public string TimestampFormat { get { throw null; } set { } } + public string? TimestampFormat { get { throw null; } set { } } [System.ObsoleteAttribute("ConsoleLoggerOptions.UseUtcTimestamp has been deprecated. Use ConsoleFormatterOptions.UseUtcTimestamp instead.")] public bool UseUtcTimestamp { get { throw null; } set { } } } diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/ref/Microsoft.Extensions.Logging.Console.csproj b/src/libraries/Microsoft.Extensions.Logging.Console/ref/Microsoft.Extensions.Logging.Console.csproj index 71aeb235fd3b0..199f8409eb161 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/ref/Microsoft.Extensions.Logging.Console.csproj +++ b/src/libraries/Microsoft.Extensions.Logging.Console/ref/Microsoft.Extensions.Logging.Console.csproj @@ -1,6 +1,7 @@ $(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum) + enable true From 9654d1d1412ed3b72aa5cc77d29bc02c9c9f1ec9 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Sat, 2 Apr 2022 00:10:51 +0300 Subject: [PATCH 03/11] DisallowNull --- .../Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs index 5c29492da1894..02afa566765f7 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Runtime.Versioning; using Microsoft.Extensions.Logging.Abstractions; @@ -20,9 +21,11 @@ internal ConsoleLogger(string name!!, ConsoleLoggerProcessor loggerProcessor) _queueProcessor = loggerProcessor; } + [DisallowNull] internal ConsoleFormatter? Formatter { get; set; } internal IExternalScopeProvider? ScopeProvider { get; set; } + [DisallowNull] internal ConsoleLoggerOptions? Options { get; set; } [ThreadStatic] From 5f371e74ec78cb19387111a57c447f742286a903 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Tue, 5 Apr 2022 00:38:49 +0300 Subject: [PATCH 04/11] Update ctor for ConsoleLoggerProcessor --- .../src/ConsoleLoggerProcessor.cs | 14 +++++++------- .../src/ConsoleLoggerProvider.cs | 12 ++++++------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProcessor.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProcessor.cs index 53f68c4d96009..8749921564a1f 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProcessor.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProcessor.cs @@ -17,13 +17,13 @@ internal class ConsoleLoggerProcessor : IDisposable private readonly BlockingCollection _messageQueue = new BlockingCollection(_maxQueuedMessages); private readonly Thread _outputThread; - [DisallowNull] - public IConsole? Console; - [DisallowNull] - public IConsole? ErrorConsole; + public IConsole Console; + public IConsole ErrorConsole; - public ConsoleLoggerProcessor() + public ConsoleLoggerProcessor(IConsole console, IConsole errorConsole) { + Console = console; + ErrorConsole = errorConsole; // Start Console message queue processor _outputThread = new Thread(ProcessLogQueue) { @@ -56,8 +56,8 @@ public virtual void EnqueueMessage(LogMessageEntry message) // for testing internal virtual void WriteMessage(LogMessageEntry entry) { - IConsole? console = entry.LogAsError ? ErrorConsole : Console; - console!.Write(entry.Message); + IConsole console = entry.LogAsError ? ErrorConsole : Console; + console.Write(entry.Message); } private void ProcessLogQueue() diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs index ff3b85eacad5d..7b4bbe6e9fe12 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs @@ -47,18 +47,18 @@ public ConsoleLoggerProvider(IOptionsMonitor options, IEnu ReloadLoggerOptions(options.CurrentValue); _optionsReloadToken = _options.OnChange(ReloadLoggerOptions); - _messageQueue = new ConsoleLoggerProcessor(); - + IConsole? console, errorConsole = null; if (DoesConsoleSupportAnsi()) { - _messageQueue.Console = new AnsiLogConsole(); - _messageQueue.ErrorConsole = new AnsiLogConsole(stdErr: true); + console = new AnsiLogConsole(); + errorConsole = new AnsiLogConsole(stdErr: true); } else { - _messageQueue.Console = new AnsiParsingLogConsole(); - _messageQueue.ErrorConsole = new AnsiParsingLogConsole(stdErr: true); + console = new AnsiParsingLogConsole(); + errorConsole = new AnsiParsingLogConsole(stdErr: true); } + _messageQueue = new ConsoleLoggerProcessor(console, errorConsole); } [UnsupportedOSPlatformGuard("windows")] From 679cf2adc30082fbefbc8b098b4d4bdba004d5ca Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Tue, 5 Apr 2022 00:46:04 +0300 Subject: [PATCH 05/11] Update ctor for ConsoleLogger --- .../src/ConsoleLogger.cs | 23 +++++++++++-------- .../src/ConsoleLoggerProvider.cs | 7 +----- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs index 02afa566765f7..9da37390839cc 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs @@ -15,18 +15,23 @@ internal sealed class ConsoleLogger : ILogger private readonly string _name; private readonly ConsoleLoggerProcessor _queueProcessor; - internal ConsoleLogger(string name!!, ConsoleLoggerProcessor loggerProcessor) + internal ConsoleLogger( + string name!!, + ConsoleLoggerProcessor loggerProcessor, + ConsoleFormatter formatter, + IExternalScopeProvider scopeProvider, + ConsoleLoggerOptions options) { _name = name; _queueProcessor = loggerProcessor; + Formatter = formatter; + ScopeProvider = scopeProvider; + Options = options; } - [DisallowNull] - internal ConsoleFormatter? Formatter { get; set; } - internal IExternalScopeProvider? ScopeProvider { get; set; } - - [DisallowNull] - internal ConsoleLoggerOptions? Options { get; set; } + internal ConsoleFormatter Formatter { get; set; } + internal IExternalScopeProvider ScopeProvider { get; set; } + internal ConsoleLoggerOptions Options { get; set; } [ThreadStatic] private static StringWriter? t_stringWriter; @@ -43,7 +48,7 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except } t_stringWriter ??= new StringWriter(); LogEntry logEntry = new LogEntry(logLevel, _name, eventId, state, exception, formatter); - Formatter!.Write(in logEntry, ScopeProvider, t_stringWriter); + Formatter.Write(in logEntry, ScopeProvider, t_stringWriter); var sb = t_stringWriter.GetStringBuilder(); if (sb.Length == 0) @@ -56,7 +61,7 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except { sb.Capacity = 1024; } - _queueProcessor.EnqueueMessage(new LogMessageEntry(computedAnsiString, logAsError: logLevel >= Options!.LogToStandardErrorThreshold)); + _queueProcessor.EnqueueMessage(new LogMessageEntry(computedAnsiString, logAsError: logLevel >= Options.LogToStandardErrorThreshold)); } public bool IsEnabled(LogLevel logLevel) diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs index 7b4bbe6e9fe12..aab663134f30b 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs @@ -150,12 +150,7 @@ public ILogger CreateLogger(string name) return _loggers.TryGetValue(name, out ConsoleLogger? logger) ? logger : - _loggers.GetOrAdd(name, new ConsoleLogger(name, _messageQueue) - { - Options = _options.CurrentValue, - ScopeProvider = _scopeProvider, - Formatter = logFormatter, - }); + _loggers.GetOrAdd(name, new ConsoleLogger(name, _messageQueue, logFormatter, _scopeProvider, _options.CurrentValue)); } #pragma warning disable CS0618 From bb48860d37977a578f909f423050c9dd39512a8c Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Tue, 5 Apr 2022 00:47:28 +0300 Subject: [PATCH 06/11] Revert spelling error --- .../src/SystemdConsoleFormatter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/SystemdConsoleFormatter.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/SystemdConsoleFormatter.cs index 11fffdc304338..9ee390ed6ebfa 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/SystemdConsoleFormatter.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/SystemdConsoleFormatter.cs @@ -45,7 +45,7 @@ public override void Write(in LogEntry logEntry, IExternalScopeP string category = logEntry.Category; int eventId = logEntry.EventId.Id; Exception? exception = logEntry.Exception; - // system reads messages from standard out line-by-line in a 'message' format. + // systemd reads messages from standard out line-by-line in a 'message' format. // newline characters are treated as message delimiters, so we must replace them. // Messages longer than the journal LineMax setting (default: 48KB) are cropped. // Example: From 139df6ad566f0dd287674a2bc38390f3f50248a9 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Tue, 5 Apr 2022 00:49:32 +0300 Subject: [PATCH 07/11] formatters can be null --- .../ref/Microsoft.Extensions.Logging.Console.cs | 2 +- .../src/ConsoleLoggerProvider.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/ref/Microsoft.Extensions.Logging.Console.cs b/src/libraries/Microsoft.Extensions.Logging.Console/ref/Microsoft.Extensions.Logging.Console.cs index f882ecc5ecc8b..c6ecd63606257 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/ref/Microsoft.Extensions.Logging.Console.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/ref/Microsoft.Extensions.Logging.Console.cs @@ -72,7 +72,7 @@ public ConsoleLoggerOptions() { } public partial class ConsoleLoggerProvider : Microsoft.Extensions.Logging.ILoggerProvider, Microsoft.Extensions.Logging.ISupportExternalScope, System.IDisposable { public ConsoleLoggerProvider(Microsoft.Extensions.Options.IOptionsMonitor options) { } - public ConsoleLoggerProvider(Microsoft.Extensions.Options.IOptionsMonitor options, System.Collections.Generic.IEnumerable formatters) { } + public ConsoleLoggerProvider(Microsoft.Extensions.Options.IOptionsMonitor options, System.Collections.Generic.IEnumerable? formatters) { } public Microsoft.Extensions.Logging.ILogger CreateLogger(string name) { throw null; } public void Dispose() { } public void SetScopeProvider(Microsoft.Extensions.Logging.IExternalScopeProvider scopeProvider) { } diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs index aab663134f30b..810a42e97f6e4 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs @@ -38,7 +38,7 @@ public ConsoleLoggerProvider(IOptionsMonitor options) /// /// The options to create instances with. /// Log formatters added for insteaces. - public ConsoleLoggerProvider(IOptionsMonitor options, IEnumerable formatters) + public ConsoleLoggerProvider(IOptionsMonitor options, IEnumerable? formatters) { _options = options; _loggers = new ConcurrentDictionary(); From 5d4d166bdc643766de40a56e5327da8bf5927432 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Tue, 5 Apr 2022 11:46:30 +0300 Subject: [PATCH 08/11] Fix tests --- .../src/ConsoleLogger.cs | 4 +-- .../ConsoleFormatterTests.cs | 20 +++++++------- .../ConsoleLoggerTest.cs | 27 ++++++++----------- 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs index 9da37390839cc..608e7bbcbfad0 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs @@ -19,7 +19,7 @@ internal ConsoleLogger( string name!!, ConsoleLoggerProcessor loggerProcessor, ConsoleFormatter formatter, - IExternalScopeProvider scopeProvider, + IExternalScopeProvider? scopeProvider, ConsoleLoggerOptions options) { _name = name; @@ -30,7 +30,7 @@ internal ConsoleLogger( } internal ConsoleFormatter Formatter { get; set; } - internal IExternalScopeProvider ScopeProvider { get; set; } + internal IExternalScopeProvider? ScopeProvider { get; set; } internal ConsoleLoggerOptions Options { get; set; } [ThreadStatic] diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/ConsoleFormatterTests.cs b/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/ConsoleFormatterTests.cs index 78e17092a5f0e..8c4b68ba6193b 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/ConsoleFormatterTests.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/ConsoleFormatterTests.cs @@ -38,37 +38,35 @@ internal static (ConsoleLogger Logger, ConsoleSink Sink, ConsoleSink ErrorSink, var errorSink = new ConsoleSink(); var console = new TestConsole(sink); var errorConsole = new TestConsole(errorSink); - var consoleLoggerProcessor = new TestLoggerProcessor(); - consoleLoggerProcessor.Console = console; - consoleLoggerProcessor.ErrorConsole = errorConsole; + var consoleLoggerProcessor = new TestLoggerProcessor(console, errorConsole); - var logger = new ConsoleLogger(_loggerName, consoleLoggerProcessor); - logger.ScopeProvider = new LoggerExternalScopeProvider(); - logger.Options = options ?? new ConsoleLoggerOptions(); var formatters = new ConcurrentDictionary(ConsoleLoggerTest.GetFormatters(simpleOptions, systemdOptions, jsonOptions).ToDictionary(f => f.Name)); + ConsoleFormatter? formatter = null; + var loggerOptions = options ?? new ConsoleLoggerOptions(); Func levelAsString; int writesPerMsg; - switch (logger.Options.FormatterName) + switch (loggerOptions.FormatterName) { case ConsoleFormatterNames.Simple: levelAsString = ConsoleLoggerTest.LogLevelAsStringDefault; writesPerMsg = 2; - logger.Formatter = formatters[ConsoleFormatterNames.Simple]; + formatter = formatters[ConsoleFormatterNames.Simple]; break; case ConsoleFormatterNames.Systemd: levelAsString = ConsoleLoggerTest.GetSyslogSeverityString; writesPerMsg = 1; - logger.Formatter = formatters[ConsoleFormatterNames.Systemd]; + formatter = formatters[ConsoleFormatterNames.Systemd]; break; case ConsoleFormatterNames.Json: levelAsString = ConsoleLoggerTest.GetJsonLogLevelString; writesPerMsg = 1; - logger.Formatter = formatters[ConsoleFormatterNames.Json]; + formatter = formatters[ConsoleFormatterNames.Json]; break; default: - throw new ArgumentOutOfRangeException(nameof(logger.Options.FormatterName)); + throw new ArgumentOutOfRangeException(nameof(loggerOptions.FormatterName)); } + var logger = new ConsoleLogger(_loggerName, consoleLoggerProcessor, formatter, new LoggerExternalScopeProvider(), loggerOptions); return (logger, sink, errorSink, levelAsString, writesPerMsg); } diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/ConsoleLoggerTest.cs b/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/ConsoleLoggerTest.cs index ebe9985a245d2..7ab86fbf509ac 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/ConsoleLoggerTest.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/ConsoleLoggerTest.cs @@ -45,32 +45,30 @@ private static (ConsoleLogger Logger, ConsoleSink Sink, ConsoleSink ErrorSink, F var errorSink = new ConsoleSink(); var console = new TestConsole(sink); var errorConsole = new TestConsole(errorSink); - var consoleLoggerProcessor = new TestLoggerProcessor(); - consoleLoggerProcessor.Console = console; - consoleLoggerProcessor.ErrorConsole = errorConsole; + var consoleLoggerProcessor = new TestLoggerProcessor(console, errorConsole); - var logger = new ConsoleLogger(_loggerName, consoleLoggerProcessor); - logger.ScopeProvider = new LoggerExternalScopeProvider(); - logger.Options = options ?? new ConsoleLoggerOptions(); var formatters = new ConcurrentDictionary(GetFormatters().ToDictionary(f => f.Name)); + ConsoleFormatter? formatter = null; + var loggerOptions = options ?? new ConsoleLoggerOptions(); Func levelAsString; int writesPerMsg; - switch (logger.Options.Format) + switch (loggerOptions.Format) { case ConsoleLoggerFormat.Default: levelAsString = LogLevelAsStringDefault; writesPerMsg = 2; - logger.Formatter = formatters[ConsoleFormatterNames.Simple]; + formatter = formatters[ConsoleFormatterNames.Simple]; break; case ConsoleLoggerFormat.Systemd: levelAsString = GetSyslogSeverityString; writesPerMsg = 1; - logger.Formatter = formatters[ConsoleFormatterNames.Systemd]; + formatter = formatters[ConsoleFormatterNames.Systemd]; break; default: - throw new ArgumentOutOfRangeException(nameof(logger.Options.Format)); + throw new ArgumentOutOfRangeException(nameof(loggerOptions.Format)); } + var logger = new ConsoleLogger(_loggerName, consoleLoggerProcessor, formatter, new LoggerExternalScopeProvider(), loggerOptions); UpdateFormatterOptions(logger.Formatter, logger.Options); VerifyDeprecatedPropertiesUsedOnNullFormatterName(logger); @@ -1115,14 +1113,11 @@ public void LogAfterDisposeWritesLog() // Arrange var sink = new ConsoleSink(); var console = new TestConsole(sink); - var processor = new ConsoleLoggerProcessor(); - processor.Console = console; + var processor = new ConsoleLoggerProcessor(console, null!); var formatters = new ConcurrentDictionary(GetFormatters().ToDictionary(f => f.Name)); - var logger = new ConsoleLogger(_loggerName, loggerProcessor: processor); - logger.Options = new ConsoleLoggerOptions(); + var logger = new ConsoleLogger(_loggerName, loggerProcessor: processor, formatters[ConsoleFormatterNames.Simple], null, new ConsoleLoggerOptions()); Assert.Null(logger.Options.FormatterName); - logger.Formatter = formatters[ConsoleFormatterNames.Simple]; UpdateFormatterOptions(logger.Formatter, logger.Options); // Act processor.Dispose(); @@ -1374,7 +1369,7 @@ private string CreateHeader(ConsoleLoggerFormat format, int eventId = 0) internal class TestLoggerProcessor : ConsoleLoggerProcessor { - public TestLoggerProcessor() + public TestLoggerProcessor(IConsole console, IConsole errorConsole) : base(console, errorConsole) { } From 5e1de4c29b44b2f26b3322a157cdd0a7c773ca4c Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 6 Apr 2022 20:42:07 +0300 Subject: [PATCH 09/11] Update src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs Co-authored-by: Eric Erhardt --- .../src/ConsoleLoggerProvider.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs index 810a42e97f6e4..d24a935e858c6 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs @@ -47,7 +47,8 @@ public ConsoleLoggerProvider(IOptionsMonitor options, IEnu ReloadLoggerOptions(options.CurrentValue); _optionsReloadToken = _options.OnChange(ReloadLoggerOptions); - IConsole? console, errorConsole = null; + IConsole? console; + IConsole? errorConsole; if (DoesConsoleSupportAnsi()) { console = new AnsiLogConsole(); From 6481b7fdb7db9573b3af67d72be8bd2bde19e4f8 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 6 Apr 2022 20:42:52 +0300 Subject: [PATCH 10/11] Update src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProcessor.cs Co-authored-by: Eric Erhardt --- .../src/ConsoleLoggerProcessor.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProcessor.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProcessor.cs index 8749921564a1f..86864142d3ecb 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProcessor.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProcessor.cs @@ -17,8 +17,8 @@ internal class ConsoleLoggerProcessor : IDisposable private readonly BlockingCollection _messageQueue = new BlockingCollection(_maxQueuedMessages); private readonly Thread _outputThread; - public IConsole Console; - public IConsole ErrorConsole; + public IConsole Console { get; } + public IConsole ErrorConsole { get; } public ConsoleLoggerProcessor(IConsole console, IConsole errorConsole) { From b7e875289649fe73171a282169a83f9f2740bb14 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 6 Apr 2022 22:08:14 +0300 Subject: [PATCH 11/11] Remove `#nullable enable` --- .../Common/src/Extensions/Logging/NullExternalScopeProvider.cs | 2 -- src/libraries/Common/src/Extensions/Logging/NullScope.cs | 2 -- src/libraries/Common/src/Interop/Windows/Interop.Libraries.cs | 2 -- .../src/Interop/Windows/Kernel32/Interop.GetConsoleMode.cs | 2 -- .../Common/src/Interop/Windows/Kernel32/Interop.GetStdHandle.cs | 2 -- .../Common/src/System/Text/Json/PooledByteBufferWriter.cs | 2 -- 6 files changed, 12 deletions(-) diff --git a/src/libraries/Common/src/Extensions/Logging/NullExternalScopeProvider.cs b/src/libraries/Common/src/Extensions/Logging/NullExternalScopeProvider.cs index 5c50de4dd6207..f7631ab6f1273 100644 --- a/src/libraries/Common/src/Extensions/Logging/NullExternalScopeProvider.cs +++ b/src/libraries/Common/src/Extensions/Logging/NullExternalScopeProvider.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable enable - using System; namespace Microsoft.Extensions.Logging diff --git a/src/libraries/Common/src/Extensions/Logging/NullScope.cs b/src/libraries/Common/src/Extensions/Logging/NullScope.cs index 83ba07d84b9b4..7748734de07be 100644 --- a/src/libraries/Common/src/Extensions/Logging/NullScope.cs +++ b/src/libraries/Common/src/Extensions/Logging/NullScope.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable enable - using System; namespace Microsoft.Extensions.Logging diff --git a/src/libraries/Common/src/Interop/Windows/Interop.Libraries.cs b/src/libraries/Common/src/Interop/Windows/Interop.Libraries.cs index 55e0128f58340..81b31db1cff99 100644 --- a/src/libraries/Common/src/Interop/Windows/Interop.Libraries.cs +++ b/src/libraries/Common/src/Interop/Windows/Interop.Libraries.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable enable - internal static partial class Interop { internal static partial class Libraries diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetConsoleMode.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetConsoleMode.cs index 6b30aa5d5fff5..1b2ee78b33ae6 100644 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetConsoleMode.cs +++ b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetConsoleMode.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable enable - using System; using System.Runtime.InteropServices; diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetStdHandle.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetStdHandle.cs index bf781b15407af..e763e01c82196 100644 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetStdHandle.cs +++ b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetStdHandle.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable enable - using System; using System.Runtime.InteropServices; diff --git a/src/libraries/Common/src/System/Text/Json/PooledByteBufferWriter.cs b/src/libraries/Common/src/System/Text/Json/PooledByteBufferWriter.cs index 64923524da448..8694a785ad3a5 100644 --- a/src/libraries/Common/src/System/Text/Json/PooledByteBufferWriter.cs +++ b/src/libraries/Common/src/System/Text/Json/PooledByteBufferWriter.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable enable - using System.Buffers; using System.Diagnostics; using System.Diagnostics.CodeAnalysis;