diff --git a/src/libraries/Microsoft.Extensions.Logging.Configuration/src/LoggerFilterConfigureOptions.cs b/src/libraries/Microsoft.Extensions.Logging.Configuration/src/LoggerFilterConfigureOptions.cs index 8a4507cfe4e19..6afe56fe68294 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Configuration/src/LoggerFilterConfigureOptions.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Configuration/src/LoggerFilterConfigureOptions.cs @@ -30,7 +30,7 @@ private void LoadDefaultConfigValues(LoggerFilterOptions options) return; } - options.CaptureScopes = GetCaptureScopesValue(options); + options.CaptureScopes = _configuration.GetValue(nameof(options.CaptureScopes), options.CaptureScopes); foreach (IConfigurationSection configurationSection in _configuration.GetChildren()) { @@ -50,8 +50,6 @@ private void LoadDefaultConfigValues(LoggerFilterOptions options) } } } - - bool GetCaptureScopesValue(LoggerFilterOptions options) => _configuration.GetValue(nameof(options.CaptureScopes), options.CaptureScopes); } private static void LoadRules(LoggerFilterOptions options, IConfigurationSection configurationSection, string? logger) diff --git a/src/libraries/Microsoft.Extensions.Logging.Configuration/src/Microsoft.Extensions.Logging.Configuration.csproj b/src/libraries/Microsoft.Extensions.Logging.Configuration/src/Microsoft.Extensions.Logging.Configuration.csproj index fa787baa1962a..545e2867e4de6 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Configuration/src/Microsoft.Extensions.Logging.Configuration.csproj +++ b/src/libraries/Microsoft.Extensions.Logging.Configuration/src/Microsoft.Extensions.Logging.Configuration.csproj @@ -37,7 +37,7 @@ - + diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleFormatterOptions.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleFormatterOptions.cs index 408afda399db9..2b3d0128b72cd 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleFormatterOptions.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleFormatterOptions.cs @@ -32,22 +32,6 @@ public ConsoleFormatterOptions() { } /// public bool UseUtcTimestamp { get; set; } - internal virtual void Configure(IConfiguration configuration) - { - if (ConsoleLoggerOptions.ParseBool(configuration, nameof(IncludeScopes), out bool includeScopes)) - { - IncludeScopes = includeScopes; - } - - if (configuration[nameof(TimestampFormat)] is string timestampFormat) - { - TimestampFormat = timestampFormat; - } - - if (ConsoleLoggerOptions.ParseBool(configuration, nameof(UseUtcTimestamp), out bool useUtcTimestamp)) - { - UseUtcTimestamp = useUtcTimestamp; - } - } + internal virtual void Configure(IConfiguration configuration) => configuration.Bind(this); } } diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerConfigureOptions.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerConfigureOptions.cs index dc4b98b0a02ff..b8cd54a488d86 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerConfigureOptions.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerConfigureOptions.cs @@ -26,6 +26,6 @@ public ConsoleLoggerConfigureOptions(ILoggerProviderConfiguration options.Configure(_configuration); + public void Configure(ConsoleLoggerOptions options) => _configuration.Bind(options); } } diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerExtensions.Obsolete.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerExtensions.Obsolete.cs index 3ddc4586e9833..4e8d31a3a7395 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerExtensions.Obsolete.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerExtensions.Obsolete.cs @@ -87,14 +87,14 @@ private ConsoleLoggerSettingsAdapter(IConsoleLoggerSettings settings) IChangeToken IOptionsChangeTokenSource.GetChangeToken() => _settings.ChangeToken ?? NullChangeToken.Instance; - string IOptionsChangeTokenSource.Name => Microsoft.Extensions.Options.Options.DefaultName; + string IOptionsChangeTokenSource.Name => Options.Options.DefaultName; void IConfigureOptions.Configure(ConsoleLoggerOptions options) { options.IncludeScopes = _settings.IncludeScopes; if (_settings is ConfigurationConsoleLoggerSettings configSettings) { - options.Configure(configSettings._configuration); + configSettings._configuration.Bind(options); } else if (_settings is ConsoleLoggerSettings consoleSettings) { @@ -105,7 +105,7 @@ void IConfigureOptions.Configure(ConsoleLoggerOptions opti internal static OptionsMonitor GetOptionsMonitor(IConsoleLoggerSettings settings) { ConsoleLoggerSettingsAdapter adapter = new(settings); - OptionsFactory factory = new( new IConfigureOptions[] { adapter }, Array.Empty>()); + OptionsFactory factory = new(new IConfigureOptions[] { adapter }, Array.Empty>()); IOptionsChangeTokenSource[] sources = new IOptionsChangeTokenSource[] { adapter }; OptionsCache cache = new(); diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerOptions.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerOptions.cs index 8b833ad6eff0e..69704e4095a8e 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerOptions.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerOptions.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; -using System.Globalization; -using Microsoft.Extensions.Configuration; namespace Microsoft.Extensions.Logging.Console { @@ -102,146 +100,5 @@ public int MaxQueueLength _maxQueuedMessages = value; } } - - internal void Configure(IConfiguration configuration) - { -#pragma warning disable CS0618 // Type or member is obsolete - if (ParseBool(configuration, nameof(DisableColors), out bool disableColors)) - { - DisableColors = disableColors; - } -#pragma warning restore CS0618 // Type or member is obsolete - -#pragma warning disable CS0618 // Type or member is obsolete - if (ParseEnum(configuration, nameof(Format), out ConsoleLoggerFormat format)) - { - Format = format; - } -#pragma warning restore CS0618 // Type or member is obsolete - - if (configuration[nameof(FormatterName)] is string formatterName) - { - FormatterName = formatterName; - } - -#pragma warning disable CS0618 // Type or member is obsolete - if (ParseBool(configuration, nameof(IncludeScopes), out bool includeScopes)) - { - IncludeScopes = includeScopes; - } -#pragma warning restore CS0618 // Type or member is obsolete - - if (ParseEnum(configuration, nameof(LogToStandardErrorThreshold), out LogLevel logToStandardErrorThreshold)) - { - LogToStandardErrorThreshold = logToStandardErrorThreshold; - } - - if (ParseInt(configuration, nameof(MaxQueueLength), out int maxQueueLength)) - { - MaxQueueLength = maxQueueLength; - } - - if (ParseEnum(configuration, nameof(QueueFullMode), out ConsoleLoggerQueueFullMode queueFullMode)) - { - QueueFullMode = queueFullMode; - } - -#pragma warning disable CS0618 // Type or member is obsolete - if (configuration[nameof(TimestampFormat)] is string timestampFormat) - { - TimestampFormat = timestampFormat; - } -#pragma warning restore CS0618 // Type or member is obsolete - -#pragma warning disable CS0618 // Type or member is obsolete - if (ParseBool(configuration, nameof(UseUtcTimestamp), out bool useUtcTimestamp)) - { - UseUtcTimestamp = useUtcTimestamp; - } -#pragma warning restore CS0618 // Type or member is obsolete - } - - /// - /// Parses the configuration value at the specified key into a bool. - /// - /// true if the value was successfully found and parsed. false if the key wasn't found. - /// Thrown when invalid data was found at the specified configuration key. - internal static bool ParseBool(IConfiguration configuration, string key, out bool value) - { - if (configuration[key] is string valueString) - { - try - { - value = bool.Parse(valueString); - return true; - } - catch (Exception e) - { - ThrowInvalidConfigurationException(configuration, key, typeof(bool), e); - } - } - - value = default; - return false; - } - - /// - /// Parses the configuration value at the specified key into an enum. - /// - /// true if the value was successfully found and parsed. false if the key wasn't found. - /// Thrown when invalid data was found at the specified configuration key. - internal static bool ParseEnum(IConfiguration configuration, string key, out T value) where T : struct - { - if (configuration[key] is string valueString) - { - try - { - value = -#if NETFRAMEWORK || NETSTANDARD2_0 - (T)Enum.Parse(typeof(T), valueString, ignoreCase: true); -#else - Enum.Parse(valueString, ignoreCase: true); -#endif - return true; - } - catch (Exception e) - { - ThrowInvalidConfigurationException(configuration, key, typeof(T), e); - } - } - - value = default; - return false; - } - - /// - /// Parses the configuration value at the specified key into an int. - /// - /// true if the value was successfully found and parsed. false if the key wasn't found. - /// Thrown when invalid data was found at the specified configuration key. - internal static bool ParseInt(IConfiguration configuration, string key, out int value) - { - if (configuration[key] is string valueString) - { - try - { - value = int.Parse(valueString, NumberStyles.Integer, NumberFormatInfo.InvariantInfo); - return true; - } - catch (Exception e) - { - ThrowInvalidConfigurationException(configuration, key, typeof(int), e); - } - } - - value = default; - return false; - } - - private static void ThrowInvalidConfigurationException(IConfiguration configuration, string key, Type valueType, Exception innerException) - { - IConfigurationSection section = configuration.GetSection(key); - throw new InvalidOperationException(SR.Format(SR.InvalidConfigurationData, section.Path, valueType), innerException); - } } } diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatterOptions.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatterOptions.cs index 2bdf51c67a847..2a9d4e5901a49 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatterOptions.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatterOptions.cs @@ -21,31 +21,6 @@ public JsonConsoleFormatterOptions() { } /// public JsonWriterOptions JsonWriterOptions { get; set; } - internal override void Configure(IConfiguration configuration) - { - base.Configure(configuration); - - if (configuration.GetSection(nameof(JsonWriterOptions)) is IConfigurationSection jsonWriterOptionsConfig) - { - JsonWriterOptions jsonWriterOptions = JsonWriterOptions; - - if (ConsoleLoggerOptions.ParseBool(jsonWriterOptionsConfig, nameof(JsonWriterOptions.Indented), out bool indented)) - { - jsonWriterOptions.Indented = indented; - } - - if (ConsoleLoggerOptions.ParseInt(jsonWriterOptionsConfig, nameof(JsonWriterOptions.MaxDepth), out int maxDepth)) - { - jsonWriterOptions.MaxDepth = maxDepth; - } - - if (ConsoleLoggerOptions.ParseBool(jsonWriterOptionsConfig, nameof(JsonWriterOptions.SkipValidation), out bool skipValidation)) - { - jsonWriterOptions.SkipValidation = skipValidation; - } - - JsonWriterOptions = jsonWriterOptions; - } - } + internal override void Configure(IConfiguration configuration) => configuration.Bind(this); } } 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 ed78c7dce5f78..8cec9d1b8ca78 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 @@ -7,29 +7,24 @@ $(DefineConstants);NO_SUPPRESS_GC_TRANSITION true true + true + + $(NoWarn);SYSLIB1100 Console logger provider implementation for Microsoft.Extensions.Logging. - - - - + + + + - - - - + + + + @@ -67,4 +62,8 @@ + + + + diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/SimpleConsoleFormatterOptions.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/SimpleConsoleFormatterOptions.cs index d31efbeda54e0..326fb16d752bb 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/SimpleConsoleFormatterOptions.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/SimpleConsoleFormatterOptions.cs @@ -25,19 +25,6 @@ public SimpleConsoleFormatterOptions() { } /// public bool SingleLine { get; set; } - internal override void Configure(IConfiguration configuration) - { - base.Configure(configuration); - - if (ConsoleLoggerOptions.ParseEnum(configuration, nameof(ColorBehavior), out LoggerColorBehavior colorBehavior)) - { - ColorBehavior = colorBehavior; - } - - if (ConsoleLoggerOptions.ParseBool(configuration, nameof(SingleLine), out bool singleLine)) - { - SingleLine = singleLine; - } - } + internal override void Configure(IConfiguration configuration) => configuration.Bind(this); } }