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
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.
精彩评论