Log4net: creating a Logger instance

guys. I have a question regarding the use of loggers in log4net. When choosing between a logger per class (static readonly field) and a logger per instance (read-only field), which is better? Personally, the only drawback I see with having a log for each class is creating it:

log4net.LogManager.GetLogger(
        System.Reflection.MethodBase.GetCurrentMethod().DeclaringType

      

It is not very pleasant because of the reflection. If I create a logger like this:

log4net.LogManager.GetLogger(typeof(MyClass))

      

chances are I'll accidentally make copy / paste errors and instead of typeof (MyClass) I can provide typeof (SomeOtherClass) which is bad.

When using a single instance log, I can use:

log4net.LogManager.GetLogger(this.GetType())

      

This approach does not use reflection and does not contain copy / paste errors.

Are there any other thoughts on this?

+3


source to share


1 answer


Apart from the fact that it would be better to use dependency injection, I think your approach is good. I've used this approach in the past.



+3


source







All Articles