log4net:未调用自定义 PatternLayoutConverter
情况:我想显示记录消息的代码的方法和行号。问题是我有一个调用 log4net 记录器的中间类。不幸的是,由于现有的架构问题,我无法取消这个中间类。结果是我总是看到方法和行号位于中间类中。不是我想要的。
所以我尝试根据这个问题创建一个自定义 PatternLayoutConverter:
我也以编程方式配置 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()));
}
}
这是我配置记录器的代码:
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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是我忘记在patternLayout 上调用ActivateOptions()。当然,我会在写完一个很长的问题后立即弄清楚这一点。
Problem was that I forgot to call ActivateOptions() on the patternLayout. Naturally, I'd figure that out right after writing up a long question.