log4net彩色控制台无法正常工作

时间:2016-06-07 12:59:57

标签: c# log4net

我正在将log4net用于两个不同的日志文件,而对于彩色控制台,文件工作正常,但控制台始终为空。

我在app.config中

  <log4net>
    <root>
      <priority value="ALL" />
      <appender-ref ref="ConsoleOutAppender" />
      <appender-ref ref="PortalLoggerAppender" />
      <appender-ref ref="ApplicationLoggerAppender" />
    </root>

    <appender name="ConsoleOutAppender" type="log4net.Appender.ColoredConsoleAppender">
 <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="ApplicationLogger" />
      </filter>
      <mapping>
        <level value="ERROR" />
        <foreColor value="Red, HighIntensity" />
      </mapping>
      <mapping>
        <level value="WARN" />
        <foreColor value="Yellow, HighIntensity" />
      </mapping>
      <mapping>
        <level value="INFO" />
        <foreColor value="Cyan" />
      </mapping>
      <mapping>
        <level value="DEBUG" />
        <foreColor value="Green" />
      </mapping>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{dd/MM HH:mm:ss} [%level] %newline%message%newline" />
      </layout>
    </appender>

    <appender name="PortalLoggerAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="Results/PortalLog_%property{DateTime}_app.log" />
      <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="PortalLogger" />
      </filter>
      <encoding value="utf-8" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <maxSizeRollBackups value="-1" />
      <maximumFileSize value="20MB" />
      <countDirection value="1" />
      <datePattern value="yyyyMMdd" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{dd/MM HH:mm:ss} [%level] %newline%message%newline" />
      </layout>
    </appender>

    <appender name="ApplicationLoggerAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="Results/ApplicationLog_%property{DateTime}_app.log" />
      <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="ApplicationLogger" />
      </filter>
      <encoding value="utf-8" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <maxSizeRollBackups value="-1" />
      <maximumFileSize value="20MB" />
      <countDirection value="1" />
      <datePattern value="yyyyMMdd" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{dd/MM HH:mm:ss} [%level] %newline%message%newline" />
      </layout>
    </appender>

    <logger name="PortalLogger" additivity="false">
      <level value="ALL"/>
      <appender-ref ref="PortalLoggerAppender"/>
    </logger>

    <logger name="ApplicationLogger" additivity="false">
      <level value="ALL"/>
      <appender-ref ref="ApplicationLoggerAppender"/>
    </logger>


    </log4net>

知道为什么log4net没有打印到控制台? 我希望应用程序记录器也将在控制台中编写。 先谢谢你。

3 个答案:

答案 0 :(得分:1)

您已将additivity专门设为false,其中as per the docs表示:

  

在此记录器的祖先中找到的appender未使用

意味着忽略顶部root中配置的那些。删除additivity或将其设置为true(默认值),或者将控制台appender显式添加到此记录器中。

答案 1 :(得分:0)

好的,我知道了我刚刚将控制台appender添加到应用程序记录器中:)

 <logger name="ApplicationLogger" additivity="false">
      <level value="ALL"/>
      <appender-ref ref="ApplicationLoggerAppender"/>
      <appender-ref ref="ConsoleOutAppender"/>
    </logger>

答案 2 :(得分:0)

要使用log4net彩色控制台,请执行以下步骤:

  1. 安装以下nuget:

安装软件包Log4Net_Logging-版本1.1.0

  1. 使用下面的log4net.config
      <appender name="Console" type="log4net.Appender.ManagedColoredConsoleAppender">
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date %-5level: %message%newline" />
        </layout>
        <mapping>
          <level value="FATAL" />
          <foreColor value="White" />
          <backColor value="Red" />
        </mapping>
        <mapping>
          <level value="ERROR" />
          <foreColor value="Red" />
        </mapping>
        <mapping>
          <level value="WARN" />
          <foreColor value="Yellow" />
        </mapping>
        <mapping>
          <level value="INFO" />
          <foreColor value="Gray" />
          <backColor value="Black" />
        </mapping>
        <mapping>
          <level value="DEBUG" />
          <foreColor value="DarkBlue" />
          <backColor value="Gray" />
        </mapping>
      </appender>

      <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="Log.txt" />
        <appendToFile value="false" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="10" />
        <maximumFileSize value="50MB" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date %-5level[%logger]: %message%newline" />
        </layout>
      </appender>

      <root>
        <level value="INFO" />
        <appender-ref ref="Console" />
        <appender-ref ref="RollingFileAppender" />
      </root>
    </log4net>
  1. 在控制器中设置
        {
            Log.SetUp(Assembly.GetEntryAssembly(), "log4net.config");
        }
  1. 开始记录:
 [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {


            Log.Info(this, "This is Info logging");
            Log.Debug(this, "This is Debug logging");
            Log.Error(this, "This is Error logging");
            Log.War(this, "This is warning");


            return new string[] { "value1", "value2" };
        }