Skip to content

Commit

Permalink
Add AddZLoggerConsole(..., bool outputToErrorStream = false)
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Sep 1, 2022
1 parent eac3b40 commit 0eac78c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ logging.AddZLoggerConsole();

It is useful for ConsoleApplication and containerized applications on cloud. For example, AWS CloudWatch, GCP Stackdriver Logging, and Datadog Logs agent collect data on the standard output. This is especially useful for debugging and analysis if these are output in a structured log.

If `consoleOutputEncodingToUtf8 = true`(default is true), set `Console.OutputEncoding = new UTF8Encoding(false)` when the provider is created.
If `consoleOutputEncodingToUtf8 = true`(default is true), set `Console.OutputEncoding = new UTF8Encoding(false)` when the provider is created. If `outputToErrorStream = false`(default is false), use standard error output.

### File

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ public ZLoggerConsoleLoggerProvider(IOptionsMonitor<ZLoggerOptions> options)
}

public ZLoggerConsoleLoggerProvider(bool consoleOutputEncodingToUtf8, string optionName, IOptionsMonitor<ZLoggerOptions> options)
: this(consoleOutputEncodingToUtf8, false, optionName, options)
{
}

public ZLoggerConsoleLoggerProvider(bool consoleOutputEncodingToUtf8, bool outputToErrorStream, string optionName, IOptionsMonitor<ZLoggerOptions> options)
{
if (consoleOutputEncodingToUtf8)
{
Console.OutputEncoding = new UTF8Encoding(false);
}

var opt = options.Get(optionName ?? DefaultOptionName);
this.streamWriter = new AsyncStreamLineMessageWriter(Console.OpenStandardOutput(), opt);
var stream = outputToErrorStream ? Console.OpenStandardError() : Console.OpenStandardOutput();
this.streamWriter = new AsyncStreamLineMessageWriter(stream, opt);
}

public ILogger CreateLogger(string categoryName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,26 @@ namespace ZLogger
{
public static class ZLoggerLoggingBuilderExtensions
{
public static ILoggingBuilder AddZLoggerConsole(this ILoggingBuilder builder, bool consoleOutputEncodingToUtf8 = true, bool configureEnableAnsiEscapeCode = false)
public static ILoggingBuilder AddZLoggerConsole(this ILoggingBuilder builder, bool consoleOutputEncodingToUtf8 = true, bool configureEnableAnsiEscapeCode = false, bool outputToErrorStream = false)
{
if (configureEnableAnsiEscapeCode)
{
EnableAnsiEscapeCode();
}

builder.AddConfiguration();
builder.Services.Add(ServiceDescriptor.Singleton<ILoggerProvider, ZLoggerConsoleLoggerProvider>(x => new ZLoggerConsoleLoggerProvider(consoleOutputEncodingToUtf8, null, x.GetService<IOptionsMonitor<ZLoggerOptions>>())));
builder.Services.Add(ServiceDescriptor.Singleton<ILoggerProvider, ZLoggerConsoleLoggerProvider>(x => new ZLoggerConsoleLoggerProvider(consoleOutputEncodingToUtf8, outputToErrorStream, null, x.GetService<IOptionsMonitor<ZLoggerOptions>>())));
LoggerProviderOptions.RegisterProviderOptions<ZLoggerOptions, ZLoggerConsoleLoggerProvider>(builder.Services);

return builder;
}

public static ILoggingBuilder AddZLoggerConsole(this ILoggingBuilder builder, Action<ZLoggerOptions> configure, bool consoleOutputEncodingToUtf8 = true, bool configureEnableAnsiEscapeCode = false)
public static ILoggingBuilder AddZLoggerConsole(this ILoggingBuilder builder, Action<ZLoggerOptions> configure, bool consoleOutputEncodingToUtf8 = true, bool configureEnableAnsiEscapeCode = false, bool outputToErrorStream = false)
{
if (configureEnableAnsiEscapeCode)
{
EnableAnsiEscapeCode();
}

return AddZLoggerConsole(builder, ZLoggerConsoleLoggerProvider.DefaultOptionName, configure, consoleOutputEncodingToUtf8);
return AddZLoggerConsole(builder, ZLoggerConsoleLoggerProvider.DefaultOptionName, configure, consoleOutputEncodingToUtf8: consoleOutputEncodingToUtf8, configureEnableAnsiEscapeCode: configureEnableAnsiEscapeCode, outputToErrorStream: outputToErrorStream);
}

public static ILoggingBuilder AddZLoggerConsole(this ILoggingBuilder builder, string optionName, Action<ZLoggerOptions> configure, bool consoleOutputEncodingToUtf8 = true, bool configureEnableAnsiEscapeCode = false)
public static ILoggingBuilder AddZLoggerConsole(this ILoggingBuilder builder, string optionName, Action<ZLoggerOptions> configure, bool consoleOutputEncodingToUtf8 = true, bool configureEnableAnsiEscapeCode = false, bool outputToErrorStream = false)
{
if (configureEnableAnsiEscapeCode)
{
Expand All @@ -48,7 +43,7 @@ public static ILoggingBuilder AddZLoggerConsole(this ILoggingBuilder builder, st
}

builder.AddConfiguration();
builder.Services.Add(ServiceDescriptor.Singleton<ILoggerProvider, ZLoggerConsoleLoggerProvider>(x => new ZLoggerConsoleLoggerProvider(consoleOutputEncodingToUtf8, optionName, x.GetService<IOptionsMonitor<ZLoggerOptions>>())));
builder.Services.Add(ServiceDescriptor.Singleton<ILoggerProvider, ZLoggerConsoleLoggerProvider>(x => new ZLoggerConsoleLoggerProvider(consoleOutputEncodingToUtf8, outputToErrorStream, optionName, x.GetService<IOptionsMonitor<ZLoggerOptions>>())));
LoggerProviderOptions.RegisterProviderOptions<ZLoggerOptions, ZLoggerConsoleLoggerProvider>(builder.Services);

builder.Services.AddOptions<ZLoggerOptions>(optionName).Configure(configure);
Expand Down
8 changes: 7 additions & 1 deletion src/ZLogger/Providers/ZLoggerConsoleLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ public ZLoggerConsoleLoggerProvider(IOptionsMonitor<ZLoggerOptions> options)
}

public ZLoggerConsoleLoggerProvider(bool consoleOutputEncodingToUtf8, string? optionName, IOptionsMonitor<ZLoggerOptions> options)
: this(consoleOutputEncodingToUtf8, false, optionName, options)
{
}

public ZLoggerConsoleLoggerProvider(bool consoleOutputEncodingToUtf8, bool outputToErrorStream, string? optionName, IOptionsMonitor<ZLoggerOptions> options)
{
if (consoleOutputEncodingToUtf8)
{
Console.OutputEncoding = new UTF8Encoding(false);
}

var opt = options.Get(optionName ?? DefaultOptionName);
this.streamWriter = new AsyncStreamLineMessageWriter(Console.OpenStandardOutput(), opt);
var stream = outputToErrorStream ? Console.OpenStandardError() : Console.OpenStandardOutput();
this.streamWriter = new AsyncStreamLineMessageWriter(stream, opt);
}

public ILogger CreateLogger(string categoryName)
Expand Down
17 changes: 6 additions & 11 deletions src/ZLogger/ZLoggerLoggingBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,26 @@ namespace ZLogger
{
public static class ZLoggerLoggingBuilderExtensions
{
public static ILoggingBuilder AddZLoggerConsole(this ILoggingBuilder builder, bool consoleOutputEncodingToUtf8 = true, bool configureEnableAnsiEscapeCode = false)
public static ILoggingBuilder AddZLoggerConsole(this ILoggingBuilder builder, bool consoleOutputEncodingToUtf8 = true, bool configureEnableAnsiEscapeCode = false, bool outputToErrorStream = false)
{
if (configureEnableAnsiEscapeCode)
{
EnableAnsiEscapeCode();
}

builder.AddConfiguration();
builder.Services.Add(ServiceDescriptor.Singleton<ILoggerProvider, ZLoggerConsoleLoggerProvider>(x => new ZLoggerConsoleLoggerProvider(consoleOutputEncodingToUtf8, null, x.GetService<IOptionsMonitor<ZLoggerOptions>>())));
builder.Services.Add(ServiceDescriptor.Singleton<ILoggerProvider, ZLoggerConsoleLoggerProvider>(x => new ZLoggerConsoleLoggerProvider(consoleOutputEncodingToUtf8, outputToErrorStream, null, x.GetService<IOptionsMonitor<ZLoggerOptions>>())));
LoggerProviderOptions.RegisterProviderOptions<ZLoggerOptions, ZLoggerConsoleLoggerProvider>(builder.Services);

return builder;
}

public static ILoggingBuilder AddZLoggerConsole(this ILoggingBuilder builder, Action<ZLoggerOptions> configure, bool consoleOutputEncodingToUtf8 = true, bool configureEnableAnsiEscapeCode = false)
public static ILoggingBuilder AddZLoggerConsole(this ILoggingBuilder builder, Action<ZLoggerOptions> configure, bool consoleOutputEncodingToUtf8 = true, bool configureEnableAnsiEscapeCode = false, bool outputToErrorStream = false)
{
if (configureEnableAnsiEscapeCode)
{
EnableAnsiEscapeCode();
}

return AddZLoggerConsole(builder, ZLoggerConsoleLoggerProvider.DefaultOptionName, configure, consoleOutputEncodingToUtf8);
return AddZLoggerConsole(builder, ZLoggerConsoleLoggerProvider.DefaultOptionName, configure, consoleOutputEncodingToUtf8: consoleOutputEncodingToUtf8, configureEnableAnsiEscapeCode: configureEnableAnsiEscapeCode, outputToErrorStream: outputToErrorStream);
}

public static ILoggingBuilder AddZLoggerConsole(this ILoggingBuilder builder, string optionName, Action<ZLoggerOptions> configure, bool consoleOutputEncodingToUtf8 = true, bool configureEnableAnsiEscapeCode = false)
public static ILoggingBuilder AddZLoggerConsole(this ILoggingBuilder builder, string optionName, Action<ZLoggerOptions> configure, bool consoleOutputEncodingToUtf8 = true, bool configureEnableAnsiEscapeCode = false, bool outputToErrorStream = false)
{
if (configureEnableAnsiEscapeCode)
{
Expand All @@ -48,7 +43,7 @@ public static ILoggingBuilder AddZLoggerConsole(this ILoggingBuilder builder, st
}

builder.AddConfiguration();
builder.Services.Add(ServiceDescriptor.Singleton<ILoggerProvider, ZLoggerConsoleLoggerProvider>(x => new ZLoggerConsoleLoggerProvider(consoleOutputEncodingToUtf8, optionName, x.GetService<IOptionsMonitor<ZLoggerOptions>>())));
builder.Services.Add(ServiceDescriptor.Singleton<ILoggerProvider, ZLoggerConsoleLoggerProvider>(x => new ZLoggerConsoleLoggerProvider(consoleOutputEncodingToUtf8, outputToErrorStream, optionName, x.GetService<IOptionsMonitor<ZLoggerOptions>>())));
LoggerProviderOptions.RegisterProviderOptions<ZLoggerOptions, ZLoggerConsoleLoggerProvider>(builder.Services);

builder.Services.AddOptions<ZLoggerOptions>(optionName).Configure(configure);
Expand Down

0 comments on commit 0eac78c

Please sign in to comment.