这个log4.net配置有什么问题?

时间:2018-01-30 20:14:05

标签: wpf log4net log4net-configuration

看起来确实应该有效。它不会抛出任何错误,逐步执行每行代码,但不会生成任何日志文件。这有什么不对?它应该在appender名称的目录中生成一个日志文件,但是不会生成任何日志文件。

log4net.xml文件:

<configuration>
  <log4net debug="true">
    <appender name ="task_appender" type="log4net.Appender.RollingAppender">
      <file value="C:\Users\ryan\Documents\Visual Studio 2017\Workout Project\GPWorkouts\blablabla.txt"></file>
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBacks value="2"/>
      <maximumFileSize value="5000KB"/>
      <staticLogFileName value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"></conversionPattern>
      </layout>   
    </appender>

    <root>
      <level value="DEBUG"/>
    </root>

    <logger name="Task">
      <level value="DEBUG"/>
      <appender-ref ref="task_appender"/>
    </logger>

  </log4net>
</configuration>

申请文件:

 public partial class MainWindow : Window
    {

        static protected ILog log = LogManager.GetLogger("Task");

        static void log4net_demo() 
        {
            FileInfo fi = new FileInfo("log4net.xml");
            log4net.Config.XmlConfigurator.Configure(fi);
            log4net.GlobalContext.Properties["host"] = Environment.MachineName;
        }

        public MainWindow()
        {
            InitializeComponent();

            log4net_demo();
            log.Info("This is the information log level");
            log.Debug("This is the debugging log level");
            log.Error("This is the error log level");
            log.Fatal("This is the fatal log level");


        }

1 个答案:

答案 0 :(得分:1)

看起来你的log4net配置中有一些拼写错误,正确的appender类型名称为log4net.Appender.RollingFileAppender

在您的xml中,它以log4net.Appender.RollingAppender

的形式给出

这是更正的xml

<configuration>
  <log4net debug="true">
    <appender name ="task_appender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\Users\ryan\Documents\Visual Studio 2017\Workout Project\GPWorkouts\blablabla.txt"></file>
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBacks value="2"/>
      <maximumFileSize value="5000KB"/>
      <staticLogFileName value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"></conversionPattern>
      </layout>   
    </appender>

    <root>
      <level value="DEBUG"/>
    </root>

    <logger name="Task">
      <level value="DEBUG"/>
      <appender-ref ref="task_appender"/>
    </logger>

  </log4net>
</configuration>