NetEscapades.Extensions.Logging
NetEscapades.Extensions.Logging copied to clipboard
Respect the convention of reading options from configuration
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().
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.