forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove ConfigurationBinder usage from Console Logging
This allows ConfigurationBinder, and its dependencies like TypeConverter, to be trimmed in an application that uses Console Logging, like an ASP.NET API application. Fix dotnet#81931
- Loading branch information
Showing
7 changed files
with
293 additions
and
21 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleFormatterConfigureOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Runtime.Versioning; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging.Configuration; | ||
using Microsoft.Extensions.Logging.Console; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Extensions.Logging | ||
{ | ||
/// <summary> | ||
/// Configures a ConsoleFormatterOptions object from an IConfiguration. | ||
/// </summary> | ||
/// <remarks> | ||
/// Doesn't use ConfigurationBinder in order to allow ConfigurationBinder, and all its dependencies, | ||
/// to be trimmed. This improves app size and startup. | ||
/// </remarks> | ||
[UnsupportedOSPlatform("browser")] | ||
internal sealed class ConsoleFormatterConfigureOptions : IConfigureOptions<ConsoleFormatterOptions> | ||
{ | ||
private readonly IConfiguration _configuration; | ||
|
||
public ConsoleFormatterConfigureOptions(ILoggerProviderConfiguration<ConsoleLoggerProvider> providerConfiguration) | ||
{ | ||
_configuration = providerConfiguration.GetFormatterOptionsSection(); | ||
} | ||
|
||
public void Configure(ConsoleFormatterOptions options) => Bind(_configuration, options); | ||
|
||
public static void Bind(IConfiguration configuration, ConsoleFormatterOptions options) | ||
{ | ||
if (configuration["IncludeScopes"] is string includeScopes) | ||
{ | ||
options.IncludeScopes = bool.Parse(includeScopes); | ||
} | ||
|
||
if (configuration["TimestampFormat"] is string timestampFormat) | ||
{ | ||
options.TimestampFormat = timestampFormat; | ||
} | ||
|
||
if (configuration["UseUtcTimestamp"] is string useUtcTimestamp) | ||
{ | ||
options.UseUtcTimestamp = bool.Parse(useUtcTimestamp); | ||
} | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerConfigureOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Globalization; | ||
using System.Runtime.Versioning; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging.Configuration; | ||
using Microsoft.Extensions.Logging.Console; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Extensions.Logging | ||
{ | ||
/// <summary> | ||
/// Configures a ConsoleLoggerOptions object from an IConfiguration. | ||
/// </summary> | ||
/// <remarks> | ||
/// Doesn't use ConfigurationBinder in order to allow ConfigurationBinder, and all its dependencies, | ||
/// to be trimmed. This improves app size and startup. | ||
/// </remarks> | ||
[UnsupportedOSPlatform("browser")] | ||
internal sealed class ConsoleLoggerConfigureOptions : IConfigureOptions<ConsoleLoggerOptions> | ||
{ | ||
private readonly IConfiguration _configuration; | ||
|
||
public ConsoleLoggerConfigureOptions(ILoggerProviderConfiguration<ConsoleLoggerProvider> providerConfiguration) | ||
{ | ||
_configuration = providerConfiguration.Configuration; | ||
} | ||
|
||
public void Configure(ConsoleLoggerOptions options) | ||
{ | ||
if (_configuration["DisableColors"] is string disableColors) | ||
{ | ||
#pragma warning disable CS0618 // Type or member is obsolete | ||
options.DisableColors = bool.Parse(disableColors); | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
} | ||
|
||
if (_configuration["Format"] is string format) | ||
{ | ||
#pragma warning disable CS0618 // Type or member is obsolete | ||
options.Format = ParseEnum<ConsoleLoggerFormat>(format); | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
} | ||
|
||
if (_configuration["FormatterName"] is string formatterName) | ||
{ | ||
options.FormatterName = formatterName; | ||
} | ||
|
||
if (_configuration["IncludeScopes"] is string includeScopes) | ||
{ | ||
#pragma warning disable CS0618 // Type or member is obsolete | ||
options.IncludeScopes = bool.Parse(includeScopes); | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
} | ||
|
||
if (_configuration["LogToStandardErrorThreshold"] is string logToStandardErrorThreshold) | ||
{ | ||
options.LogToStandardErrorThreshold = ParseEnum<LogLevel>(logToStandardErrorThreshold); | ||
} | ||
|
||
if (_configuration["MaxQueueLength"] is string maxQueueLength) | ||
{ | ||
options.MaxQueueLength = int.Parse(maxQueueLength, NumberStyles.Integer, NumberFormatInfo.InvariantInfo); | ||
} | ||
|
||
if (_configuration["QueueFullMode"] is string queueFullMode) | ||
{ | ||
options.QueueFullMode = ParseEnum<ConsoleLoggerQueueFullMode>(queueFullMode); | ||
} | ||
|
||
if (_configuration["TimestampFormat"] is string timestampFormat) | ||
{ | ||
#pragma warning disable CS0618 // Type or member is obsolete | ||
options.TimestampFormat = timestampFormat; | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
} | ||
|
||
if (_configuration["UseUtcTimestamp"] is string useUtcTimestamp) | ||
{ | ||
#pragma warning disable CS0618 // Type or member is obsolete | ||
options.UseUtcTimestamp = bool.Parse(useUtcTimestamp); | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
} | ||
} | ||
|
||
public static T ParseEnum<T>(string value) where T : struct => | ||
#if NETSTANDARD || NETFRAMEWORK | ||
(T)Enum.Parse(typeof(T), value, ignoreCase: true); | ||
#else | ||
Enum.Parse<T>(value, ignoreCase: true); | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...ibraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatterConfigureOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Globalization; | ||
using System.Runtime.Versioning; | ||
using System.Text.Json; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging.Configuration; | ||
using Microsoft.Extensions.Logging.Console; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Extensions.Logging | ||
{ | ||
/// <summary> | ||
/// Configures a JsonConsoleFormatterOptions object from an IConfiguration. | ||
/// </summary> | ||
/// <remarks> | ||
/// Doesn't use ConfigurationBinder in order to allow ConfigurationBinder, and all its dependencies, | ||
/// to be trimmed. This improves app size and startup. | ||
/// </remarks> | ||
[UnsupportedOSPlatform("browser")] | ||
internal sealed class JsonConsoleFormatterConfigureOptions : IConfigureOptions<JsonConsoleFormatterOptions> | ||
{ | ||
private readonly IConfiguration _configuration; | ||
|
||
public JsonConsoleFormatterConfigureOptions(ILoggerProviderConfiguration<ConsoleLoggerProvider> providerConfiguration) | ||
{ | ||
_configuration = providerConfiguration.GetFormatterOptionsSection(); | ||
} | ||
|
||
public void Configure(JsonConsoleFormatterOptions options) | ||
{ | ||
ConsoleFormatterConfigureOptions.Bind(_configuration, options); | ||
|
||
if (_configuration.GetSection("JsonWriterOptions") is IConfigurationSection jsonWriterOptionsConfig) | ||
{ | ||
JsonWriterOptions jsonWriterOptions = options.JsonWriterOptions; | ||
|
||
if (jsonWriterOptionsConfig["Indented"] is string indented) | ||
{ | ||
jsonWriterOptions.Indented = bool.Parse(indented); | ||
} | ||
|
||
if (jsonWriterOptionsConfig["MaxDepth"] is string maxDepth) | ||
{ | ||
jsonWriterOptions.MaxDepth = int.Parse(maxDepth, NumberStyles.Integer, NumberFormatInfo.InvariantInfo); | ||
} | ||
|
||
if (jsonWriterOptionsConfig["SkipValidation"] is string skipValidation) | ||
{ | ||
jsonWriterOptions.SkipValidation = bool.Parse(skipValidation); | ||
} | ||
|
||
options.JsonWriterOptions = jsonWriterOptions; | ||
} | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...raries/Microsoft.Extensions.Logging.Console/src/SimpleConsoleFormatterConfigureOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Runtime.Versioning; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging.Configuration; | ||
using Microsoft.Extensions.Logging.Console; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Extensions.Logging | ||
{ | ||
/// <summary> | ||
/// Configures a SimpleConsoleFormatterOptions object from an IConfiguration. | ||
/// </summary> | ||
/// <remarks> | ||
/// Doesn't use ConfigurationBinder in order to allow ConfigurationBinder, and all its dependencies, | ||
/// to be trimmed. This improves app size and startup. | ||
/// </remarks> | ||
[UnsupportedOSPlatform("browser")] | ||
internal sealed class SimpleConsoleFormatterConfigureOptions : IConfigureOptions<SimpleConsoleFormatterOptions> | ||
{ | ||
private readonly IConfiguration _configuration; | ||
|
||
public SimpleConsoleFormatterConfigureOptions(ILoggerProviderConfiguration<ConsoleLoggerProvider> providerConfiguration) | ||
{ | ||
_configuration = providerConfiguration.GetFormatterOptionsSection(); | ||
} | ||
|
||
public void Configure(SimpleConsoleFormatterOptions options) | ||
{ | ||
ConsoleFormatterConfigureOptions.Bind(_configuration, options); | ||
|
||
if (_configuration["ColorBehavior"] is string colorBehavior) | ||
{ | ||
options.ColorBehavior = ConsoleLoggerConfigureOptions.ParseEnum<LoggerColorBehavior>(colorBehavior); | ||
} | ||
|
||
if (_configuration["SingleLine"] is string singleLine) | ||
{ | ||
options.SingleLine = bool.Parse(singleLine); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters