Nlog Change class name

I am wondering if there is a way to change the class name of the log instance at runtime? I want not to create a registrar in each class, but to enter it through the constructor. but after that I get the wrong class name in the file that raises 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 will be fine, but in the 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

+4
source share
1 answer

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 lead to the expected result.

+2
source

All Articles