سؤال

Just wondering if I am doing this right:
I declare Log4Net object:

private static readonly log4net.ILog _log = log4net.LogManager.GetLogger
        (System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

And then in methods in whole class when log I just use:

_log.Debug("passing...");

Is this ok or I should declare and use Log4Net object inside methods (instatiate _log in every method)?

هل كانت مفيدة؟

المحلول

The way you do it is better than declaring the logger in every method. The name of the method you are in can be found again by walking up the call stack, which is described here so initializing logs in each method would be counterproductive since reflection would eat some time at each call.

Even with a static object the reflection calls to resolve your class names take some resources but until you time them you won't know how they impact you. If you start to observe a long delay at startup it may be because the loggers initialisation is run as the classes types are resolved. If so then you will need to optimize. But for now, don't bother, you're doing it right

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top