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

loggerFactory.WithFilters doesn't work as expected #525

Closed
MichaCo opened this issue Nov 29, 2016 · 10 comments
Closed

loggerFactory.WithFilters doesn't work as expected #525

MichaCo opened this issue Nov 29, 2016 · 10 comments

Comments

@MichaCo
Copy link

MichaCo commented Nov 29, 2016

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.

@justcurious
Copy link

justcurious commented Jan 25, 2017

I wouldn't have expected that to work. What should work is either:

loggerFactory = new LoggerFactory().WithFilter(new FilterLoggerSettings()
{
    { "Test", LogLevel.Error }
});

or

loggerFactory = new LoggerFactory();
loggerFactory = loggerFactory.WithFilter(new FilterLoggerSettings()
{
    { "Test", LogLevel.Error }
});

@MichaCo
Copy link
Author

MichaCo commented Jan 25, 2017

@justcurious Yeah, well, that's just, like, your opinion, man.
On a serious note, sure your 2 examples work, but that's not the point

a) It is not obvious from the API that one call to AddStuff behaves different than WithFilter. In most fluent builders you modify the original or build a new one. But doing both is kinda...
b) it doesn't work well at all with DI.

@HaoK
Copy link
Member

HaoK commented Jun 14, 2017

@pakrym is this also supposed to be fixed now?

@MichaCo
Copy link
Author

MichaCo commented Jun 14, 2017

@HaoK It's just me but from the looks of things with the changes to global filters, yup ;)

@pakrym
Copy link
Contributor

pakrym commented Jun 14, 2017

We removed old WithFilter API. There is only one LoggerFactory now and AddFilter appends to it's rules.

@HaoK
Copy link
Member

HaoK commented Jun 14, 2017

Cool, we should close this issue as fixed in preview 2 then :)

@pakrym
Copy link
Contributor

pakrym commented Jun 14, 2017

Fixed as pard of #626 . AddFilter now adds a rule to the main logger factory

@pakrym pakrym closed this as completed Jun 14, 2017
@RobertoPrevato
Copy link

RobertoPrevato commented Jan 25, 2018

Hi @pakrym ,
I know I am commenting on a old thread, but I just installed official Microsoft.Extensions.Logging.Filter package from Microsoft; and LoggerFactory does have WithFilter method, it doesn't have AddFilter. Wasn't it the same class you were speaking about?

@pakrym
Copy link
Contributor

pakrym commented Jan 25, 2018

@RobertoPrevato what version of the package do you have? AddFilter is on LoggerBuilder ( see sample at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?tabs=aspnetcore2x#log-filtering)

@RobertoPrevato
Copy link

@pakrym, thanks for answering. I installed the latest version ("Microsoft.Extensions.Logging.Filter" Version="1.1.2"). I am actually using this package for a NET Core 2.0 console application, not an ASP.NET application; does that make any difference?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants