adams85 / filelogger

A lightweight yet feature-rich file logger implementation for the Microsoft.Extensions.Logging framework.
MIT License
147 stars 22 forks source link

AddJsonFile() doesn't configure custom FileLoggerProvider #22

Closed KostiaChorny closed 2 years ago

KostiaChorny commented 2 years ago

Hey,

when I try to add JSON file logger for my custom logger provider, it doesn't take into account optionsName when call Configure:

var textBuilder = new JsonFileLogEntryTextBuilder(new JsonWriterOptions { Indented = false });
loggingBuilder.AddJsonFile<ErrorFileLoggerProvider>(textBuilder: textBuilder, configure: options =>
{
    options.RootPath = AppContext.BaseDirectory;
});

AddJsonFile() calls JsonFileLoggerExtensions.ConfigureTextBuilder() under the hood and there optionsName is not passed to Configure method

 builder.Services.Configure(delegate (FileLoggerOptions options)
 {
      options.TextBuilder = textBuilder;
 });

so it configures default options but not the specific for the provider. The same issue with AddJsonFile method itself

public static ILoggingBuilder AddJsonFile<TProvider>(this ILoggingBuilder builder, FileLoggerContext context = null, JsonFileLogEntryTextBuilder textBuilder = null, Action<FileLoggerOptions> configure = null, string optionsName = null) where TProvider : FileLoggerProvider
        {
            builder.AddFile<TProvider>(context, null, optionsName).ConfigureTextBuilder(textBuilder);
            if (configure != null)
            {
                builder.Services.Configure(configure);
            }

            return builder;
        }

builder.Services.Configure(configure); configures default options but not the providers one.

As a workaround I've added custom provider the following way:

var textBuilder = new JsonFileLogEntryTextBuilder(new JsonWriterOptions { Indented = false });
var optionsName = typeof(ErrorFileLoggerProvider).ToString();
    loggingBuilder.AddFile<ErrorFileLoggerProvider>(optionsName: optionsName);
    builder.Services.Configure(optionsName, (FileLoggerOptions options) =>
    {
        options.TextBuilder = textBuilder;
        options.RootPath = AppContext.BaseDirectory;
    });

I would be really nice if you can fix it. Thanks for the great logger!

adams85 commented 2 years ago

Hi!

Thanks for reporting and investigating this. Fixed by v3.3.1, which will be available on NuGet soon.

KostiaChorny commented 2 years ago

Thank you!