Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net argument to LogManager.GetLogger

Tags:

log4net

Why do most log4net examples get the logger for a class by doing this:

private static ILog logger = 
    LogManager.GetLogger(
    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

Rather than just passing typeof(MyClass):

private static ILog logger = LogManager.GetLogger(typeof(MyClass));

Is there any other reason for doing this, beside the fact that the first option does not require you to type a specific class name?

like image 775
Andy White Avatar asked Mar 25 '09 02:03

Andy White


People also ask

What is LogManager getLogger?

The getLogger() method of java. util. logging. LogManager is used to get the specified Logger in this LogManager instance. This Logger must be a named Logger.

Is log4net dependent on log4j?

Log4net is a logging utility for . NET applications. It's based on log4j, which is for Java applications. Log4net is highly configurable, so you can use it in many scenarios.


4 Answers

I think you've got the reason. I do it that way so I don't have to worry about the class name and can just copy and paste boiler plate code in a new class.

For the official answer, see: How do I get the fully-qualified name of a class in a static block? at the log4net faq

like image 159
Steven Lyons Avatar answered Oct 18 '22 17:10

Steven Lyons


I'm an NLog user, and usually this boils down to :

var _logger = LogManager.GetCurrentClassLogger();

It seemed a bit strange that you need to go through reflection in Log4Net, so I had a look in the NLog source code, and lo and behold, this is what they do for you:

[MethodImpl(MethodImplOptions.NoInlining)]
public static Logger GetCurrentClassLogger()
{
    string loggerName;
    Type declaringType;
    int framesToSkip = 1;
    do
    {
#if SILVERLIGHT
        StackFrame frame = new StackTrace().GetFrame(framesToSkip);
#else
        StackFrame frame = new StackFrame(framesToSkip, false);
#endif
        var method = frame.GetMethod();
        declaringType = method.DeclaringType;
        if (declaringType == null)
        {
            loggerName = method.Name;
            break;
        }
        framesToSkip++;
        loggerName = declaringType.FullName;
    } while (declaringType.Module.Name.Equals("mscorlib.dll", StringComparison.OrdinalIgnoreCase));
    return globalFactory.GetLogger(loggerName);
}

I guess I would write something similar for Log4Net as an extension or static method instead of pasting the reflection as part of my boiler code :)

like image 40
Noctis Avatar answered Oct 18 '22 16:10

Noctis


As you say - its convenient as you can create a logger in a method without knowing the name of the class (trivial I know) but allows you to cut and paste methods between classes without having to rename the call.

like image 26
Preet Sangha Avatar answered Oct 18 '22 18:10

Preet Sangha


I think the reason is, that you get the type of the runtime type using the .DeclaringType() method. You can use the logger in a base class and still see the actual type of your object in the loggers output. That makes investigations much more convinient.

like image 25
PeterB Avatar answered Oct 18 '22 18:10

PeterB