Gotta love the guys that would rather pick at the question than answer it ... sigh. In case anyone else needs this
public static class ConsoleLoggerExtensions
{
public static ILoggingBuilder AddCustomFormatter(
this ILoggingBuilder builder) =>
builder.AddConsole(options => options.FormatterName = nameof(CustomLoggingFormatter))
.AddConsoleFormatter<CustomLoggingFormatter, ConsoleFormatterOptions>();
}
public sealed class CustomLoggingFormatter : ConsoleFormatter, IDisposable
{
private readonly IDisposable _optionsReloadToken;
private ConsoleFormatterOptions _formatterOptions;
public CustomLoggingFormatter(IOptionsMonitor<ConsoleFormatterOptions> options)
// Case insensitive
: base(nameof(CustomLoggingFormatter)) =>
(_optionsReloadToken, _formatterOptions) =
(options.OnChange(ReloadLoggerOptions), options.CurrentValue);
private void ReloadLoggerOptions(ConsoleFormatterOptions options) =>
_formatterOptions = options;
public override void Write<TState>(
in LogEntry<TState> logEntry,
IExternalScopeProvider scopeProvider,
TextWriter textWriter)
{
string? message =
logEntry.Formatter?.Invoke(
logEntry.State, logEntry.Exception);
if (message is null)
{
return;
}
textWriter.WriteLine($"{message}");
}
public void Dispose() => _optionsReloadToken?.Dispose();
}
then in .ConfigureServices(services =>
services.AddLogging(opt =>
{
opt.AddCustomFormatter();
}