尽管我添加了控制台附加程序,但log4net不会打印到控制台

时间:2019-04-04 11:20:27

标签: c# log4net

请参阅log4net配置(App.config文件):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <log4net>
    <appender name="Console" type="log4net.Appender.ColoredConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%date] [%level] [%logger/%method] --> %message%newline%exception"/>
      </layout>
      <mapping>
        <level value="DEBUG" />
        <foreColor value="Cyan, HighIntensity" />
      </mapping>
      <mapping>
        <level value="INFO" />
        <foreColor value="Green, HighIntensity" />
      </mapping>
      <mapping>
        <level value="WARN" />
        <foreColor value="Yellow, HighIntensity" />
      </mapping>
      <mapping>
        <level value="ERROR" />
        <foreColor value="Red, HighIntensity" />
      </mapping>
    </appender>
    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
      <file value="c:\logs\log.txt"/>
      <rollingStyle value="Date" />
      <appendToFile value="true" />
      <rollingStyle value="Size"/>
      <maximumFileSize value="10MB"/>
      <maxSizeRollBackups value="5"/>
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <datePattern value="yyyyMMdd" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%date] [%level] - %message%newline%exception"/>
      </layout>
    </appender>
    <root>
      <level value="Info" />
      <appender-ref ref="Console" />
      <appender-ref ref="RollingFile" />
    </root>
  </log4net>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
</configuration>

LogHelper

public class LogHelper
{
    public static log4net.ILog GetLogger([CallerFilePath]string fileName = "")
    {
        return log4net.LogManager.GetLogger(fileName);
    }
}

主要

class Program
{
    private static readonly log4net.ILog log = LogHelper.GetLogger("Main");
    static void Main(string[] args)
    {
        log.Info("Bla bla");
        Console.ReadLine();
    }
}

那很奇怪,我只是看不到我的log

1 个答案:

答案 0 :(得分:2)

您需要在日志帮助器中的某个地方调用log4net.Config.XmlConfigurator.Configure();