Nlog: Rule with final and minlevel, followed by catch all

Created on 11 Feb 2016  路  7Comments  路  Source: NLog/NLog

I've been using NLog for avout 3 years, but lately I'm getting into more complex scenarios.
I'm confused by the rules with final, minlevel and the next rules. So either it's documentation that's missing and/or my config was wrong and/or my assumptions are wrong.

I've got a logger name "loginchecks" that gets called with trace, debug and info methods. But atm it only needs to log info level and up.

private static readonly Logger LoginChecksLogger = LogManager.GetLogger("loginchecks");
...
LoginChecksLogger.Debug("{0};{1};checks ok", this.CurrentLogin, addressBarUrl);

However the "*" logger is getting the trace and debug messages, but I thought that marking the other "loginchecks" with final would stop the message from flowing down to nameless logger.

    <logger name="loginchecks" minlevel="Info" writeTo="loginchecks" final="true" />
    <logger name="*" minlevel="Trace" writeTo="file" />

Because I'm using GetLogger, I assumed that the "*" logger would not even come into play. Then I though that marking it with final would solve it.

I already figured out I need to add an extra "loginchecks" logger without level nor writeTo attributes so the message stop there.

My questions:
Is this the intended behavior, especially since I'm using a named logger?
Am I doing something wrong?
Should the documentation be updated for cases with multiple loggers?

This is on v4.2.3

question

Most helpful comment

I had the same issue and I think perhaps the documentation needs a bit of a tweak, it was not clear to me that minLevel (or logLevel) was treated as a filter same as name.

From reading the docs it seemed that name is the filter and then I am choosing for this filter which level I want to log (and other/lower levels will be discarded).

The documentation reads:

A rule defines which log entry level(s) are logged. Entries with other levels are ignored. A commonly used specifier is minLevel. The other specifiers allow for more advanced configuration.

I (mis)understood 'ignored' to mean those are not logged, but I guess it means they don't match and fall thru to the further rules.

All 7 comments

Just on mobile, so short.

The final rules will only apply to the messages that are kept by the filter (name and LogLevel) . So loginChecks traces will reach the 2nd rule, but not loginChecks info and up.

The * means any logger, so also the logger loginChecks.

Last but not least, a logger have names. The logger created by getcurrentclasslogger will have the full name of the class.

Hope this make things clear. If not, just let us know :)

It surely answers my first question. That this is the intended behaviour.
Good point about the "*" matching any logger and that it also includes the named logger. So GetCurrentClassLogger is also a named logger, but the name is derived... from the current class.

So I guess the solution I came up with is a correct solution.
If you could confirm that, I might try to clarify the wiki/docs.

I'm not sure what you are trying to achieve. But if you don't want to log trace messages to any target, then you need a null target or a logger filter.

If this is still an issue, please let us know!

I had the same issue and I think perhaps the documentation needs a bit of a tweak, it was not clear to me that minLevel (or logLevel) was treated as a filter same as name.

From reading the docs it seemed that name is the filter and then I am choosing for this filter which level I want to log (and other/lower levels will be discarded).

The documentation reads:

A rule defines which log entry level(s) are logged. Entries with other levels are ignored. A commonly used specifier is minLevel. The other specifiers allow for more advanced configuration.

I (mis)understood 'ignored' to mean those are not logged, but I guess it means they don't match and fall thru to the further rules.

Good point. I think there is a (subtle) difference between "match" en "log" regarding the rules.

Feel free to edit the wiki :)

Thank you, I did a little edit.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MaximRouiller picture MaximRouiller  路  3Comments

npandrei picture npandrei  路  3Comments

smeegoan picture smeegoan  路  3Comments

ericnewton76 picture ericnewton76  路  3Comments

haythamabutair picture haythamabutair  路  3Comments