When logging from a derived class, should I inherit the class-logger instance from the base class, or instantiate a new one?
Namely, which is better:
public class Base
{
private static Logger _logger = LogManager.GetCurrentClassLogger();
private void Foo()
{
_logger.Info("Hello world");
}
}
public class Derived : Base
{
new private static Logger _logger = LogManager.GetCurrentClassLogger();
private void Fee()
{
_logger.Info("Hello world");
}
}
or:
public class Base
{
protected static Logger _logger = LogManager.GetCurrentClassLogger();
private void Foo()
{
_logger.Info("Hello world");
}
}
public class Derived : Base
{
private void Fee()
{
_logger.Info("Hello world");
}
}
You should use a new one.
LogManager.GetCurrentClassLogger
creates a logger whose name is the current class. If you use the parent's class logger, you lose the information of the class doing the logging, it would appear as if the logging was done by the parent class. Most of the time, you want as precise an information as possible when logging.