NetEscapades.Extensions.Logging icon indicating copy to clipboard operation
NetEscapades.Extensions.Logging copied to clipboard

Respect the convention of reading options from configuration

Open guillaume86 opened this issue 3 years ago • 1 comments

Most LoggingProviders allow to read options from the configuration with a convention (Logging:File will be the one in your case).

You can enable that by calling LoggerProviderOptions.RegisterProviderOptions<FileLoggerOptions, FileLoggerProvider>(builder.Services); in AddFile().

guillaume86 avatar Feb 05 '22 23:02 guillaume86

I think you also have to add builder.AddConfiguration().

Then, the full code for that would be:

public static ILoggingBuilder AddFile(this ILoggingBuilder builder)
{
    builder.AddConfiguration();

    builder.Services.AddSingleton<ILoggerProvider, FileLoggerProvider>();
    builder.Services.AddSingleton<ILogFormatter, SimpleLogFormatter>();
#if NETCOREAPP3_0
    builder.Services.AddSingleton<ILogFormatter, JsonLogFormatter>();
#endif

    LoggerProviderOptions.RegisterProviderOptions<FileLoggerOptions, FileLoggerProvider>(builder.Services);

    return builder;
}

Keep in Mind, that this will add additional dependencies to Microsoft.Extensions.Logging.Configuration.

DrPepperBianco avatar Jan 26 '24 11:01 DrPepperBianco