log4net logger defined in base class

Viewed 9920

I want to build my log4net logger in my MVC controller abstract base class like so:

protected static readonly ILog Log = LogManager.GetLogger(typeof(AuthorizedController));

In this manner I can define the logger once and be done with it. The only problem is that the logger attribute in the log output will always be AuthorizedController, and if I have FooController inherited from AuthorizedController I'd like the log output to reflect that.

What would be a good KISS, DRY, and efficient way do doing this?

4 Answers

As Steve Guidi suggested - use GetType to create logger for specific instance of type. You can save reference to instance of logger which gets calling LogManager.GetLogger in backing field:

    private ILog _log;
    protected ILog Log => _log ?? (_log = LogManager.GetLogger(GetType()));
Related