开发者

Logging the method or class name always write the Common.Logging method

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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜