Skip to content

How to create Logger for sub classes

Rolf Kristensen edited this page Mar 25, 2021 · 5 revisions

When we wish to expose the logger into sub classes the following pattern could be used.

class BaseClass
{
    protected BaseClass()
    {
        Log = LogManager.GetLogger(GetType().ToString());
    }

    protected Logger Log { get; private set; }
}

class ExactClass : BaseClass
{
    public ExactClass() : base() { }
    ...
}

Alternative solution could be this:

class BaseClass
{
    protected virtual Logger Log { get { return _logger; } }
    private static Logger _logger = LogManager.GetCurrentClassLogger();
}

class ExactClass : BaseClass
{
    protected override Logger Log { get { return _logger; } }
    private static Logger _logger = LogManager.GetCurrentClassLogger();
}
Clone this wiki locally