Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4net - optimal strategy when using inheritance

Tags:

c#

log4net

I have integrated log4net in my app. I have a few helper methods to assist in logging which call log4net. When refactoring, I plan to move these methods to base class so that the code is not repeated in other derived classes.

Without the inheritance model, following worked correctly in each class

private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

Placing the above in the base class will return the declaring type as base class and not derived class.

What is an optimal way to move this declaration to the base class?

At present, I can think of a few ways to achieve this but don't find them optimal.

like image 675
byte Avatar asked Oct 25 '10 14:10

byte


1 Answers

I think I would do this:

LogManager.GetLogger(this.GetType());
like image 115
Stefan Egli Avatar answered Sep 23 '22 21:09

Stefan Egli