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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You are right. There is an open bug associated for this task: sourceforge.net/tracker/…
The issue was opened on 2010-08-13 but is still opened Common.Logging 2.1.1
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.