log4net funktioniert nicht für Windows Forms-Anwendung

Ich wünschte wirklich, jemand könnte mir dabei helfen. Es ist wirklich fahrend mich verrückt.

Ich habe einen einfachen einfachen Windows Forms-Anwendung, und ich bin versucht, das log4net-Bibliothek für die Anmeldung (ich bin einfach nur testen in diesem Projekt, weil es nicht geklappt hat in meinem main-Projekt).

So, ich habe die regulären Form1.cs, app.config, AssemblyInfo.cs und Program.cs.

In meiner app.config habe ich:

<?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>

In der 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");
        }
    }
}

Und am Ende der AssemblyInfo.cs-Datei habe ich Hinzugefügt:

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

Wenn ich debug und gehen Sie dem button1_Click-ich kann sehen, dass nichts passiert ist. Das log-Objekt hat seine:

IsInfoEnabled, IsDebugEnabled, IsErrorEnabled, IsWarnEnabled auf false gesetzt und einfach nichts passiert.

Ich habe versucht eine Lösung zu finden, die den ganzen Tag lang und nichts. Kann jemand helfen?

  • Da Sie nicht über die log4net Konfiguration Abschnitt eingeführt, in dem configSections-Sektion der app.config, ich nehme Ihre app.config wird ignoriert. Andernfalls würden Sie, eine Ausnahme zu sagen, dass die log4net Abschnitt ist nicht legal in der app.config.
InformationsquelleAutor user2128702 | 2013-07-25
Schreibe einen Kommentar