4

I'm using log4net with .NET Common.Logging. I configured a message pattern to include method and class name like so:

<layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="%-5level [%M %C] - %message%newline" />
</layout>

The problem is I always get the name of the Common.Logging method and class:

FATAL [Fatal Common.Logging.Factory.AbstractLogger] - log message
INFO  [Info Common.Logging.Factory.AbstractLogger] - log message
DEBUG [Debug Common.Logging.Factory.AbstractLogger] - log message

It is most unuseful. Is there a way of printing my method name?

HuBeZa
  • 4,715
  • 3
  • 36
  • 58

2 Answers2

3

I think you need to modify Common.Logging. In the file Log4NetLogger.cs replace the following line

private readonly static Type declaringType = typeof(Log4NetLogger);

with

private readonly static Type declaringType = typeof(AbstractLogger);

This should do the trick. Here is some explanation why this should work.

Community
  • 1
  • 1
Stefan Egli
  • 17,398
  • 3
  • 54
  • 75
  • You are right. There is an open bug associated for this task: https://sourceforge.net/tracker/index.php?func=detail&aid=3044182&group_id=182424&atid=901156 – HuBeZa Jan 13 '11 at 15:11
  • The issue was opened on 2010-08-13 but is still opened Common.Logging 2.1.1 – Lee Chee Kiam Sep 28 '12 at 23:48
1

This format works with my config

<layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="%-5level [%logger] - %message%newline" />
</layout>

My logger is defined as

private static readonly Common.Logging.ILog logger = 
              Common.Logging.LogManager.GetCurrentClassLogger();

I use these unmodified dlls

  • Common.Logging.dll ver 2.0.0.0
  • Common.Logging.Log4Net.dll ver 2.0.0.0
  • log4net.dll ver 1.2.10

Update: In the logging messages i add the methodname into the logging call as text so i have no performance overhead with analysing the call stack to get the methodname.

k3b
  • 14,517
  • 7
  • 53
  • 85
  • You are right. this solves the class name problem. The method name is a much bigger one, especially since I don't want to use reflection to get the method name all the time - only when I configure it so. – HuBeZa Jan 13 '11 at 14:52