本文介绍了log4net-单个FileAppender的每级别的conversionPattern?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然在每个级别上设置不同的日志文件(每个都有不同的conversionPattern)并不困难,但我希望除错误以外的所有日志都最少,在此我希望有详细的日志条目.这是我当前配置的摘要:

While it is not difficult to setup different log files, each with a different conversionPattern, per level, I would like to have minimal logging for all but errors, where I'd like a detailed log entry. Here's a snippet of my current configuration:

<appender name="WarningsAndBelowFileAppender" type="log4net.Appender.FileAppender">
  <file value="log.txt" />
  <appendToFile value="false" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%m%n" />
  </layout>
  <filter type="log4net.Filter.LevelRangeFilter">
    <levelMax value="WARN" />
  </filter>
</appender>

<appender name="ErrorsFileAppender" type="log4net.Appender.FileAppender">
  <file value="errors.txt" />
  <appendToFile value="false" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%d [%t] %-5p %c [%x] - %m%n" />
  </layout>
  <threshold value="ERROR" />
</appender>

我很想将这两个配置都放入一个日志文件中,每个配置都有不同的conversionPatterns.能做到吗?

I'd love to have both of these configurations go into a single log file, just have different conversionPatterns for each. Can this be done?

推荐答案

您无法在配置文件中执行此操作,但可以使用模式布局转换器来创建它.

you can't do it within the config file but you can make it with pattern layout converter.

查看信息此处

这应该是转换后的代码:

This should be the converted code:

class MyMessageConverter : PatternLayoutConverter
{
    protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
    {
        if (loggingEvent.Level.Value <= Level.Warn.Value)
        {
            writer.Write(loggingEvent.RenderedMessage);
        }
        else
        {
            writer.Write("{0} [{1}] {2,5} {3} - {4}",
                loggingEvent.TimeStamp,
                loggingEvent.ThreadName,
                loggingEvent.Level.Name,
                loggingEvent.LocationInformation.ClassName,
                loggingEvent.RenderedMessage);

        }
    }
}

这是配置文件:

<appender name="FileAppender" type="log4net.Appender.FileAppender">
  <file value="log.txt" />
  <appendToFile value="false" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%MSG%n" />
      <converter>
        <name value="MSG" />
        <type value="yournamespace.MyMessageConverter " />
      </converter>
  </layout>
</appender>

这篇关于log4net-单个FileAppender的每级别的conversionPattern?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:43