user932139
user932139

Reputation: 41

NLog. Change class name

i'm wondering if there is any way to change class name of logger instance at runtime ? i want not to create logger in every class but to inject it via constructor. but after i do it i get in file wrong class name which invokes the event. Project structure is like this

public class c1
{
    private Logger _logger = LogManager.GetCurrentClassLogger();
    public void doSmth()
    {
        c2 myC2= new c2(_logger);
        myC2.LogSomething();
    }

    public void LogSomething()
    {
        _logger.Info("c1 test");
    }
}


public class c2
{
    private Logger _logger;

    public c2(Logger logger)
    {
        this._logger = logger;
    }

    public void LogSomething() {
        _logger.Info("c2 test");
    }
}

everything woks fine but in log file we get

2011-09-07 09:33:59.7521|INFO|c1|c1 test
2011-09-07 09:33:59.7611|INFO|c1|c2 test

Upvotes: 4

Views: 3005

Answers (1)

ccellar
ccellar

Reputation: 10344

Instead of the ${logger} layout renderer, use the ${callsite} renderer.

<target xsi:type="Trace" 
            name="t"  
 layout="${callsite:className=true:includeSourcePath=false:methodName=false}
 | ${message}" />

This should produce your expected result.

Upvotes: 5

Related Questions