loggerFactory.WithFilters doesn't work as expected #525
Description
Harsh title, I know, but hey, at least I was surprised and had to look at your code to figure it actually does only create a new FilteredLoggerFactory instance ;)
Given the following code
loggerFactory = new LoggerFactory();
loggerFactory.WithFilter(new FilterLoggerSettings()
{
{ "Test", LogLevel.Error }
});
loggerFactory.AddConsole();
logger = loggerFactory.CreateLogger("Test");
logger.LogInformation("Testing stuff...");
All looks fine right?
Expected result would be no log message because the filter should filter away the info message.
Unfortunately, WithFilter
only creates a new logger factory and in case you don't use that one, the messages do not get filtered.
This also applies to use cases where you want to add filtering to in your Startup (Configure) method
public void Configure(ILoggerFactory loggerFactory, IApplicationBuilder app)
{
loggerFactory.WithFilter(new FilterLoggerSettings()
{
{ "Microsoft", LogLevel.Warning }
});
This also does nothing of course.
This is not really a very critial issue because you can still inject/replace the ILoggerFactory during ConfigureServices
, but still, it is not very friendly ;)
I guess it would be better if the base logger factory would know about the filters and WithFilters just adds those.