Skip to content

Commit

Permalink
Add Overloads for Add(Json/Systemd/Simple)Console (dotnet#39725)
Browse files Browse the repository at this point in the history
  • Loading branch information
maryamariyan authored Jul 21, 2020
1 parent 69ef55e commit e87fbd1
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ public static partial class ConsoleLoggerExtensions
public static Microsoft.Extensions.Logging.ILoggingBuilder AddConsole(this Microsoft.Extensions.Logging.ILoggingBuilder builder, System.Action<Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions> configure) { throw null; }
public static Microsoft.Extensions.Logging.ILoggingBuilder AddConsoleFormatter<TFormatter, TOptions>(this Microsoft.Extensions.Logging.ILoggingBuilder builder) where TFormatter : Microsoft.Extensions.Logging.Console.ConsoleFormatter where TOptions : Microsoft.Extensions.Logging.Console.ConsoleFormatterOptions { throw null; }
public static Microsoft.Extensions.Logging.ILoggingBuilder AddConsoleFormatter<TFormatter, TOptions>(this Microsoft.Extensions.Logging.ILoggingBuilder builder, System.Action<TOptions> configure) where TFormatter : Microsoft.Extensions.Logging.Console.ConsoleFormatter where TOptions : Microsoft.Extensions.Logging.Console.ConsoleFormatterOptions { throw null; }
public static Microsoft.Extensions.Logging.ILoggingBuilder AddJsonConsole(this Microsoft.Extensions.Logging.ILoggingBuilder builder) { throw null; }
public static Microsoft.Extensions.Logging.ILoggingBuilder AddJsonConsole(this Microsoft.Extensions.Logging.ILoggingBuilder builder, System.Action<Microsoft.Extensions.Logging.Console.JsonConsoleFormatterOptions> configure) { throw null; }
public static Microsoft.Extensions.Logging.ILoggingBuilder AddSimpleConsole(this Microsoft.Extensions.Logging.ILoggingBuilder builder) { throw null; }
public static Microsoft.Extensions.Logging.ILoggingBuilder AddSimpleConsole(this Microsoft.Extensions.Logging.ILoggingBuilder builder, System.Action<Microsoft.Extensions.Logging.Console.SimpleConsoleFormatterOptions> configure) { throw null; }
public static Microsoft.Extensions.Logging.ILoggingBuilder AddSystemdConsole(this Microsoft.Extensions.Logging.ILoggingBuilder builder) { throw null; }
public static Microsoft.Extensions.Logging.ILoggingBuilder AddSystemdConsole(this Microsoft.Extensions.Logging.ILoggingBuilder builder, System.Action<Microsoft.Extensions.Logging.Console.ConsoleFormatterOptions> configure) { throw null; }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ public static ILoggingBuilder AddConsole(this ILoggingBuilder builder, Action<Co
return builder;
}

/// <summary>
/// Add the default console log formatter named 'simple' to the factory with default properties.
/// </summary>
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
public static ILoggingBuilder AddSimpleConsole(this ILoggingBuilder builder) =>
builder.AddFormatterWithName(ConsoleFormatterNames.Simple);

/// <summary>
/// Add and configure a console log formatter named 'simple' to the factory.
/// </summary>
Expand All @@ -59,6 +66,13 @@ public static ILoggingBuilder AddSimpleConsole(this ILoggingBuilder builder, Act
return builder.AddConsoleWithFormatter<SimpleConsoleFormatterOptions>(ConsoleFormatterNames.Simple, configure);
}

/// <summary>
/// Add a console log formatter named 'json' to the factory with default properties.
/// </summary>
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
public static ILoggingBuilder AddJsonConsole(this ILoggingBuilder builder) =>
builder.AddFormatterWithName(ConsoleFormatterNames.Json);

/// <summary>
/// Add and configure a console log formatter named 'json' to the factory.
/// </summary>
Expand All @@ -79,19 +93,29 @@ public static ILoggingBuilder AddSystemdConsole(this ILoggingBuilder builder, Ac
return builder.AddConsoleWithFormatter<ConsoleFormatterOptions>(ConsoleFormatterNames.Systemd, configure);
}

/// <summary>
/// Add a console log formatter named 'systemd' to the factory with default properties.
/// </summary>
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
public static ILoggingBuilder AddSystemdConsole(this ILoggingBuilder builder) =>
builder.AddFormatterWithName(ConsoleFormatterNames.Systemd);

internal static ILoggingBuilder AddConsoleWithFormatter<TOptions>(this ILoggingBuilder builder, string name, Action<TOptions> configure)
where TOptions : ConsoleFormatterOptions
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
builder.AddConsole((ConsoleLoggerOptions options) => options.FormatterName = name);
builder.AddFormatterWithName(name);
builder.Services.Configure(configure);

return builder;
}

private static ILoggingBuilder AddFormatterWithName(this ILoggingBuilder builder, string name) =>
builder.AddConsole((ConsoleLoggerOptions options) => options.FormatterName = name);

/// <summary>
/// Adds a custom console logger formatter 'TFormatter' to be configured with options 'TOptions'.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void AddSimpleConsole_ChangeProperties_IsReadFromLoggingConfiguration()
var loggerProvider = new ServiceCollection()
.AddLogging(builder => builder
.AddConfiguration(configuration)
.AddSimpleConsole(o => {})
.AddSimpleConsole()
)
.BuildServiceProvider()
.GetRequiredService<ILoggerProvider>();
Expand All @@ -186,6 +186,37 @@ public void AddSimpleConsole_ChangeProperties_IsReadFromLoggingConfiguration()
Assert.True(formatter.FormatterOptions.IncludeScopes);
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/38337", TestPlatforms.Browser)]
public void AddSimpleConsole_OutsideConfig_TakesProperty()
{
var configuration = new ConfigurationBuilder().AddInMemoryCollection(new[] {
new KeyValuePair<string, string>("Console:FormatterOptions:TimestampFormat", "HH:mm "),
new KeyValuePair<string, string>("Console:FormatterOptions:UseUtcTimestamp", "true"),
new KeyValuePair<string, string>("Console:FormatterOptions:IncludeScopes", "false"),
}).Build();

var loggerProvider = new ServiceCollection()
.AddLogging(builder => builder
.AddConfiguration(configuration)
.AddSimpleConsole(o => {
o.TimestampFormat = "HH:mm:ss ";
o.IncludeScopes = false;
o.UseUtcTimestamp = true;
})
)
.BuildServiceProvider()
.GetRequiredService<ILoggerProvider>();

var consoleLoggerProvider = Assert.IsType<ConsoleLoggerProvider>(loggerProvider);
var logger = (ConsoleLogger)consoleLoggerProvider.CreateLogger("Category");
Assert.Equal(ConsoleFormatterNames.Simple, logger.Options.FormatterName);
var formatter = Assert.IsType<SimpleConsoleFormatter>(logger.Formatter);
Assert.Equal("HH:mm:ss ", formatter.FormatterOptions.TimestampFormat);
Assert.False(formatter.FormatterOptions.IncludeScopes);
Assert.True(formatter.FormatterOptions.UseUtcTimestamp);
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/38337", TestPlatforms.Browser)]
public void AddSystemdConsole_ChangeProperties_IsReadFromLoggingConfiguration()
Expand All @@ -199,7 +230,7 @@ public void AddSystemdConsole_ChangeProperties_IsReadFromLoggingConfiguration()
var loggerProvider = new ServiceCollection()
.AddLogging(builder => builder
.AddConfiguration(configuration)
.AddSystemdConsole(o => {})
.AddSystemdConsole()
)
.BuildServiceProvider()
.GetRequiredService<ILoggerProvider>();
Expand All @@ -213,6 +244,37 @@ public void AddSystemdConsole_ChangeProperties_IsReadFromLoggingConfiguration()
Assert.True(formatter.FormatterOptions.IncludeScopes);
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/38337", TestPlatforms.Browser)]
public void AddSystemdConsole_OutsideConfig_TakesProperty()
{
var configuration = new ConfigurationBuilder().AddInMemoryCollection(new[] {
new KeyValuePair<string, string>("Console:FormatterOptions:TimestampFormat", "HH:mm "),
new KeyValuePair<string, string>("Console:FormatterOptions:UseUtcTimestamp", "true"),
new KeyValuePair<string, string>("Console:FormatterOptions:IncludeScopes", "true"),
}).Build();

var loggerProvider = new ServiceCollection()
.AddLogging(builder => builder
.AddConfiguration(configuration)
.AddSystemdConsole(o => {
o.TimestampFormat = "HH:mm:ss ";
o.IncludeScopes = false;
o.UseUtcTimestamp = false;
})
)
.BuildServiceProvider()
.GetRequiredService<ILoggerProvider>();

var consoleLoggerProvider = Assert.IsType<ConsoleLoggerProvider>(loggerProvider);
var logger = (ConsoleLogger)consoleLoggerProvider.CreateLogger("Category");
Assert.Equal(ConsoleFormatterNames.Systemd, logger.Options.FormatterName);
var formatter = Assert.IsType<SystemdConsoleFormatter>(logger.Formatter);
Assert.Equal("HH:mm:ss ", formatter.FormatterOptions.TimestampFormat);
Assert.False(formatter.FormatterOptions.UseUtcTimestamp);
Assert.False(formatter.FormatterOptions.IncludeScopes);
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/38337", TestPlatforms.Browser)]
public void AddJsonConsole_ChangeProperties_IsReadFromLoggingConfiguration()
Expand All @@ -227,7 +289,7 @@ public void AddJsonConsole_ChangeProperties_IsReadFromLoggingConfiguration()
var loggerProvider = new ServiceCollection()
.AddLogging(builder => builder
.AddConfiguration(configuration)
.AddJsonConsole(o => {})
.AddJsonConsole()
)
.BuildServiceProvider()
.GetRequiredService<ILoggerProvider>();
Expand Down

0 comments on commit e87fbd1

Please sign in to comment.