winforms log4net不适用于Windows窗体应用程序

pokxtpni  于 2022-11-17  发布在  Windows
关注(0)|答案(3)|浏览(158)

我真的希望有人能帮我。这真的让我发疯。
我有一个简单的Windows窗体应用程序,我正在尝试使用log4net库进行日志记录(我只是在这个项目中测试它,因为它在我的主项目中没有工作)。
因此,我有常规的Form1.cs、app.config、AssemblyInfo.cs和Program.cs。
在我的应用程序配置中,我有:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <log4net>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <file value="log-file.txt" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <header value="[Your Header text here]" />
        <footer value="[Your Footer text here]" />
        <conversionPattern value="%date [%thread] %-5level %logger [%ndc] 
                 &lt;%property{auth}&gt; - %message%newline" />
      </layout>
    </appender>

  </log4net>

  <startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>

在Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net;
using log4net.Config;

namespace log4net.test
{
    public partial class Form1 : Form
    {        
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public Form1()
        {

            InitializeComponent();      
        }

        private void button1_Click(object sender, EventArgs e)
        {
            log4net.Config.XmlConfigurator.Configure();
            log.Debug("This is a DEBUG level message. The most VERBOSE level.");
            log.Info("Extended information, with higher importance than the Debug call");
            log.Warn("An unexpected but recoverable situation occurred");
        }
    }
}

在AssemblyInfo.cs文件的末尾,我添加了:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "app.config", Watch = true)]

当我调试并转到button1_Click时,我可以看到没有发生任何事情。
将“已启用信息”、“已启用调试”、“已启用错误”和“已启用警告"设置为假,则不会发生任何操作。
我一整天都在想办法,但一无所获。有人能帮我吗?

mwecs4sa

mwecs4sa1#

问题就出在这一行:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "app.config", Watch = true)]

在运行时,bin目录中没有文件“app.config”。您必须指定如下正确的文件名:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "<your assembly name>.exe.config", Watch = true)]

或者最好省略名称

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

我最近遇到了一个类似的问题。如果日志不工作,我通常启用调试输出,如下所示:How to track down log4net problems

s2j5cfk0

s2j5cfk02#

您需要在配置中设置根日志记录级别:

</appender>

    <!-- Set root logger level to DEBUG and its only appender to A1 -->
    <root>
        <level value="DEBUG" />
        <appender-ref ref="A1" />
    </root>
</log4net>
rbpvctlc

rbpvctlc3#

您还需要为log4net设置configuration节。设置configuration节允许log4net从app.config读取设置。请参见以下内容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

    /// Add this section before the log4net node
    <configSections>
       <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>

   <log4net>
     <root>
       <level value="DEBUG" />
       <appender-ref ref="LogFileAppender" />
     </root>
     <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
       <file value="log-file.txt" />
       <appendToFile value="true" />
       <layout type="log4net.Layout.PatternLayout">
         <header value="[Your Header text here]" />
         <footer value="[Your Footer text here]" />
         <conversionPattern value="%date [%thread] %-5level %logger [%ndc] 
                 &lt;%property{auth}&gt; - %message%newline" />
       </layout>
     </appender>

   </log4net>

   <startup>
   <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
 </startup>
 </configuration>

相关问题