Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

loggerFactory.WithFilters doesn't work as expected #525

Closed
@MichaCo

Description

@MichaCo

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions