I would like to replicate the example given in the "Semi Dynamic Routing Rules" section of this page: https://github.com/nlog/nlog/wiki/Filtering-log-messages
Nlog.Config.LoggingRule uses Nlog.LogLevel for MinLevel and MaxLevel, so how can I use a configuration variable to dynamically set the minLevel?
Hi! Thanks for opening your first issue here! Please make sure to follow the issue template - so we could help you better!
Was thinking that people who used the config-api from code, didn't need the help of dynamic-layout-logic. Both because of the overhead, but also because the runtime validation of the Layout-result makes it more error-prone.
Instead I recommend that you just create the LoggingRule using the constructor where you can provide a ruleName. Then you can lookup the LoggingRule at later point and modify it like you want (See LoggingConfiguration.FindRuleByName).
P.S. Remember to call ReconfigureExistingLoggers after changing LoggingRules of the active Configuration,.
Alternative you can use NLog.Filters.WhenMethodFilter and implement your filtering there.
c#
config.LoggingRules.Last().Filters.Add(new WhenMethodFilter(logEvent => ShouldIgnoreLogEvent(logEvent) ? FilterResult.Ignore : FilterResult.Log));
No need to call ReconfigureExistingLoggers and it is fully dynamic (not just semi).
Have updated the Wiki for you:
@snakefoot: I needed dynamic adaptation of the minimum log level. In my first attempt, I found the LogginRule.IsLoggingEnabledForLevel and LoggingRule.EnableLoggingForLevel methods, but I overlooked the method to set the minLevel (SetLoggingLevels). So my solution was somewhat clunky.
I had to make a second pass over the code and then discovered the SetLoggingLevels - so in the end, my solution looks pretty much as you suggested in your first post.
Thanks for the additional approach - that looks pretty much like the idea I first had (but didn't find the methods that would enable me to do so.).