问题描述
情况:我想显示记录邮件的代码的方法和行号。问题是,我有一个中间类,调用log4net logger。不幸的是,由于现有的架构问题,我不能免除这个中间类。结果是我总是看到方法和行号在中间类。不是我想要的。
所以我试图创建一个自定义PatternLayoutConverter,根据这个问题:
a href =http://stackoverflow.com/questions/1906227/does-log4net-support-including-the-call-stack-in-a-log-message> log4net支持在日志消息中包括调用堆栈
我也是以编程方式配置log4net,因为再次出于架构原因,使用XML配置文件是不可行的(我还发现,只有记录的配置log4net的方式是通过一个愚蠢的XML文件,但这是另一个讨论的主题)。所以我按照这个线程。
一切正常,除了我的自定义转换器从未被调用。这是自定义转换器的代码:
public class TVPatternLayout:PatternLayout {
public TVPatternLayout(){
this.AddConverter(logsite,typeof(TVPatternLayoutConverter));
}
}
public class TVPatternLayoutConverter:PatternLayoutConverter {
protected override void Convert(TextWriter writer,LoggingEvent loggingEvent){
StackTrace st = new StackTrace );
int idx = 1;
while(st.GetFrame(idx).GetMethod()。DeclaringType.Assembly == typeof(LogManager).Assembly)
idx ++;
writer.Write(String.Format({0}。{1}(line {2}),st.GetFrame(idx).GetMethod()。DeclaringType.Name,st.GetFrame(idx)。 GetMethod()。Name,
st.GetFrame(idx).GetFileLineNumber()));
}
}
这里是我配置记录器的代码:
层次结构=(层次结构)LogManager.GetRepository
hierarchy.Configured = false;
RollingFileAppender appender = new RollingFileAppender();
appender.Name = loggerName;
appender.File = realPath;
appender.AppendToFile = true;
appender.MaximumFileSize =8000;
appender.MaxSizeRollBackups = 2;
TVPatternLayout patternLayout = new TVPatternLayout();
patternLayout.ConversionPattern = logFormat; // include%logsite,my custom option
appender.Layout = patternLayout;
appender.ActivateOptions();
hierarchy.Root.AddAppender(appender);
hierarchy.Root.Level = Level.All;
hierarchy.Configured = true;问题是,我忘了调用ActivateOptions()on()方法来调用ActivateOptions()方法。 patternLayout。当然,我在写出一个长长的问题后就会想出来。
Situation: I want to show the method and line number for the code that logs a message. The problem is that I have an intermediate class which calls into the log4net logger. Unfortunately, due to existing architectural issues, I can't do away with this intermediate class. The result is that I always see the method and line number as being in the intermediate class. Not what I want.
So I tried to create a custom PatternLayoutConverter, as per this question:
Does log4net support including the call stack in a log message
I am also programmatically configuring log4net since, again for architectural reasons, using an XML config file is not feasible (I also find it ridiculous that the preferred and only documented way of configuring log4net is through a stupid XML file, but that's a topic for another discussion). So I followed this thread.
How to configure log4net programmatically from scratch (no config)
Everything works fine except that my custom converter is never called. Here is the code for the custom converter:
public class TVPatternLayout : PatternLayout {
public TVPatternLayout() {
this.AddConverter("logsite", typeof(TVPatternLayoutConverter));
}
}
public class TVPatternLayoutConverter : PatternLayoutConverter {
protected override void Convert(TextWriter writer, LoggingEvent loggingEvent) {
StackTrace st = new StackTrace();
int idx = 1;
while(st.GetFrame(idx).GetMethod().DeclaringType.Assembly == typeof(LogManager).Assembly)
idx++;
writer.Write(String.Format("{0}.{1} (line {2})", st.GetFrame(idx).GetMethod().DeclaringType.Name, st.GetFrame(idx).GetMethod().Name,
st.GetFrame(idx).GetFileLineNumber()));
}
}
And here is the code where I configure the logger:
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Configured = false;
RollingFileAppender appender = new RollingFileAppender();
appender.Name = loggerName;
appender.File = realPath;
appender.AppendToFile = true;
appender.MaximumFileSize = "8000";
appender.MaxSizeRollBackups = 2;
TVPatternLayout patternLayout = new TVPatternLayout();
patternLayout.ConversionPattern = logFormat; // includes %logsite, my custom option
appender.Layout = patternLayout;
appender.ActivateOptions();
hierarchy.Root.AddAppender(appender);
hierarchy.Root.Level = Level.All;
hierarchy.Configured = true;
解决方案 Problem was that I forgot to call ActivateOptions() on the patternLayout. Naturally, I'd figure that out right after writing up a long question.
这篇关于log4net:未调用自定义PatternLayoutConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!