-
Notifications
You must be signed in to change notification settings - Fork 246
NET Core 2.0.0-preview2 Log filtering #654
Comments
There is the same code in article aspnet/Announcements#238
It work perfect in NET Core 2.0.0-preview1 but |
I'm using the following packages:
|
The The new loggerFactory
.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Error)
.AddFilter("Engine", LogLevel.Debug)
.AddConsole(); @pakrym Was the removal of the dictionary configuration intended? If so, the announcement post should probably be updated to reflect that. |
Ok but these methods accept only ILoggingBuilder, how can i get instance of this type? I've found that these methods can be called inside IServiceCollection's extension AddLogging
but if i'll create new LoggerFactory(); than these filters are ignored and i see full log. |
Hmm, you’re right, I missed that part. Looking at the source, it seems that those var loggerProviders = …;
var options = new LoggerFilterOptions();
options.AddRule(new LoggerFilterRule(null, "Microsoft", LogLevel.Warning, null));
options.AddRule(new LoggerFilterRule(null, "System", LogLevel.Error, null));
options.AddRule(new LoggerFilterRule(null, "Engine", LogLevel.Debug, null));
var loggerFactory = new LoggerFactory(loggerProviders, options); I would agree that this is a terrible interface though to construct a logger factory. While I welcome the changes from the announcement to make this all a bit nicer, I think there should still be a good way to construct (and set up) logger factories without a working DI environment. The way everything is built now, it does not seem like there is a nice way to construct a logger factory outside of DI, which is a shame because I personally started using the logging stuff in a lot of smaller projects that do not use DI. |
Thanks that works. Main difference is that you must use LoggerFilterOptions collection instead of AddFilter(new Dictionary<string, LogLevel>)
|
While it works, I don’t really consider it a solution. So I’d still be interested in reasons for this change and suggestions how to properly consume logging outside of DI. |
Reopening based on last comment. |
Reason for using dependency injection to compose logging is described in #626 (comment) As for configuring logging filters outside DI, creating |
Thanks for the link to the explanations and updating the announcements on this. Btw. there are two typos I’ve spotted: You wrote As for having extensions for Alternatively, what would you think about also exposing the var loggerFactory = new LoggerFactory();
loggerFactory.AddConsole();
loggerFactory.Options.AddFilter("Microsoft", LogLevel.Warning);
loggerFactory.Options.AddFilter("System", LogLevel.Error);
loggerFactory.Options.AddFilter("Engine", LogLevel.Debug); Although I’m not perfectly sure how we could properly expose the options while keeping the options monitor working properly. |
Is it possible to filter single controller action or whole controller from being logged? |
@mabakay You can set up filters with more specific namespaces or full type names, as explained in the documentation. So you could easily filter out a specific controller like |
We periodically close 'discussion' issues that have not been updated in a long period of time. We apologize if this causes any inconvenience. We ask that if you are still encountering an issue, please log a new issue with updated information and we will investigate. |
Hello, after upgrade to version 2.0.0-preview2 LoggerFactory filtering is broken and there is no method AddFilter anymore. Can you suggest how to filter built in event sources in new version or did i miss something?
Here is my ConfigureServices part:
The text was updated successfully, but these errors were encountered: