Unable to filter on class name (in NLog.config) when using anotar catel nlog logging

dromer1967

I am using anotar catel fody for logging in my application.

In NLog.config I want to use different levels for certain classes. Example config

<logger name="SpaceA.*"
        minlevel="Info"
        writeTo="file"
        final="true" />
<logger name="*"
        minlevel="Debug"
        writeTo="file" />

I have created a NLogListener class which derives from catel's LogListenerBase.

public class NLogListener : LogListenerBase
{
    private static readonly NLog.Logger Log = NLog.LogManager.GetCurrentClassLogger();

    protected override void Debug(ILog log, string message, object extraData)
    {
        Log.Debug(message);
    }

    protected override void Info(ILog log, string message, object extraData)
    {
        Log.Info(message);
    }

    protected override void Warning(ILog log, string message, object extraData)
    {
        Log.Warn(message);
    }

    protected override void Error(ILog log, string message, object extraData)
    {
        Log.Error(message);
    }
    #endregion Methods
}

In my code I use Catel Anotar Fody:

LogTo.Debug("Starting something...");

Now no matter where I use the logging, it is all being displayed as coming from the namespace where I have defined the LogListerer.

What am I doing wrong and ergo do I have to change to be able to filter the NLog on class names like it normally should?

Geert van Horrik

The problem is that you get the current class logger in the LogListener:

private static readonly NLog.Logger Log = NLog.LogManager.GetCurrentClassLogger();

That way, you always log to the NLogListener type. What you should do is get the right logger type for each entry:

protected override void Debug(ILog log, string message, object extraData)
{
    var nlog = NLog.LogManager.GetClassLogger(log.TargetType);
    nlog.Debug(message);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Adding method name in NLog

From Dev

using NLog for the first time

From Dev

Logging in multiple files using NLog

From Dev

Unable to filter on class name (in NLog.config) when using anotar catel nlog logging

From Dev

Logging datetime with ms precision using NLog event context layout renderer

From Dev

Config Transformation on NLog does not work

From Dev

NLog and Common.Logging nightmare

From Dev

Is there a way to create a named logger with NLog and filter logging by this name(s)?

From Dev

Logging to NLog in Hangfire.io

From Dev

Nlog 3.1 with Castle Windsor not logging

From Dev

logging to oracle with nLog

From Dev

Should I inherit the class logger when using NLog?

From Dev

Turn off Logging during Release from Nlog.config

From Dev

Logging with NLog and runtime parameters to Database

From Dev

NLog Console logging on single line

From Dev

Logging multiple threads with nlog

From Dev

Check NLog minlevel before logging?

From Dev

How to use variables from different class in NLog.Config?

From Dev

Nlog logging methods

From Dev

using NLog for the first time

From Dev

Logging datetime with ms precision using NLog event context layout renderer

From Dev

Adding method name in NLog

From Dev

Logging to NLog in Hangfire.io

From Dev

NLog breaks when using operator '<'

From Dev

NLog AzureAppendBlob not logging

From Dev

Check NLog minlevel before logging?

From Dev

Lifestyle of NLog wrapper class

From Dev

NLog is not logging into the database when its configured to log from code

From Dev

NLog filter to change Error to Warning