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 :)