log4net的:自定义PatternLayoutConverter不会被调用 [英] log4net: Custom PatternLayoutConverter not being called

查看:234
本文介绍了log4net的:自定义PatternLayoutConverter不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:我想显示该消息记录代码的方法和行号。问题是,我有一个调用到log4net的记录器的中间阶段。不幸的是,由于现有的架构问题,我不能在本中级班做了。其结果是,我总是看到该方法和行号作为中级班之中。 。不是我想要的。



所以,我试图创建一个自定义PatternLayoutConverter,按照这样的问题:



< A HREF =http://stackoverflow.com/questions/1906227/does-log4net-support-including-the-call-stack-in-a-log-message> http://stackoverflow.com/questions/1906227 /不-log4net的支持,包括最调用堆栈-IN-A-日志信息



我也编程方式配置log4net的,因为,再建筑方面的原因,使用XML配置文件是不可行的(我也觉得可笑的是log4net的配置的首选和唯一记载的方法是通过一个愚蠢的XML文件,但这是另一个讨论的话题)。所以我遵循了这一主题。



http://stackoverflow.com/questions/769983/how-to-configure-log4net-programmatically-from-scratch-no-config



除了我的自定义转换器永远不会被调用,一切工作正常。下面是定制转换器的代码:

 公共类TVPatternLayout:的PatternLayout {
公共TVPatternLayout(){
this.AddConverter(LOGSITE的typeof(TVPatternLayoutConverter));
}
}

公共类TVPatternLayoutConverter:PatternLayoutConverter {
保护覆盖无效转换(TextWriter的作家,LoggingEvent所LoggingEvent所){
堆栈跟踪ST =新的堆栈跟踪( );
INT IDX = 1;
,而(st.GetFrame(IDX).GetMethod()== DeclaringType.Assembly typeof运算(日志管理).Assembly。)
IDX ++;
writer.Write(的String.Format({0}。{1}(线{2}),st.GetFrame(IDX).GetMethod()。DeclaringType.Name,st.GetFrame(IDX)。 。实现getMethod()名称,
st.GetFrame(IDX).GetFileLineNumber()));
}
}

和这里是我配置记录器的代码:

 层次等级=(层次)LogManager.GetRepository(); 
hierarchy.Configured = FALSE;

RollingFileAppender进行追加程序=新RollingFileAppender进行();
appender.Name = loggerName;
appender.File =真实路径;
appender.AppendToFile = TRUE;
appender.MaximumFileSize =8000;
appender.MaxSizeRollBackups = 2;

TVPatternLayout的PatternLayout =新TVPatternLayout();
patternLayout.ConversionPattern = LOGFORMAT; //包含%LOGSITE,我的自定义选项
appender.Layout =的PatternLayout;

appender.ActivateOptions();
hierarchy.Root.AddAppender(追加程序);

hierarchy.Root.Level = Level.ALL的;
hierarchy.Configured = TRUE;


解决方案

问题是,我忘了打电话的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:

http://stackoverflow.com/questions/1906227/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.

http://stackoverflow.com/questions/769983/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不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆