public abstract class BaseClass
{
private Logger _logger;
protected Logger Logger
{
get { return _logger ?? (_logger = LogManager.GetCurrentClassLogger()); }
}
}
public class Derived : BaseClass
{
public Derived()
{
Logger.Info("test");
// Logger "BaseClass"
// Expected "Derived"
}
}
Workaround:
_logger = LogManager.GetLogger(GetType().Name)
I would expect "baseclass" because it's defined in baseclass.
This is expected behavior.
What would you expect with class Derived2: BaseClass...?
What would you expect with class Derived2: BaseClass...?
logger: Derived2
BaseClass is an abstract class. I expect that it use the instantiated class
Yes but the method is not abstract
the method says: GetCurrentClassLogger
I expect the current instantiated class, maybe I'm wrong...
So what is the best practice if i want to have a base abstract class with a logger?
The best practice it to create a private field for each class. So each class have:
private Logger _logger = LogManager.GetCurrentClassLogger(); //or static if desired.
There is no benefit creating this in an abstract class IMO
the benefit is to not have to implement a logger in all derived classes
It saves 1 line of code for each class, yes.
LogManager.GetLogger(GetType().Name) looks also fine to me.
Closed as this seems answered, please reopen if needed
Most helpful comment
the benefit is to not have to implement a logger in all derived classes