tags:

views:

26

answers:

1

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.

+1  A: 

I think I would do this:

LogManager.GetLogger(this.GetType());
Stefan Egli
Thanks Stefan. So would I need to make "Log" non static?
byte
Or in the base class constructor write something like if (BaseClass.Log == null) { BaseClass.Log = LogManager.GetLogger(this.GetType()); } // assuming Log is still private static in BaseClass
byte
It cannot be static because you need one instance per derived class.
Stefan Egli
yes, that's true.
byte