简体   繁体   中英

log4net is not logging anything nor creating the file

I'm trying to configure log4net in my WPF application but I'm struggling to do so. I have read all the questions about it here but none resolved my issue. Find the code down below.

log4net NuGet version: 2.0.8

AssemblyInfo.cs

using System.Windows;

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
                                     //(used if a resource is not found in the page,
                                     // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
                                              //(used if a resource is not found in the page,
                                              // app, or any theme specific resource dictionaries)
)]
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <log4net>
        <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
            <param name="File" value="C:\Mylogs\Installer.log" />
            <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
            <appendToFile value="true" />
            <rollingStyle value="Size" />
            <maxSizeRollBackups value="10" />
            <maximumFileSize value="1MB" />
            <staticLogFileName value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
            </layout>
        </appender>
        <root>
            <level value="ALL" />
            <appender-ref ref="LogFileAppender" />
        </root>
    </log4net>
</configuration>

MainWindow.xaml.cs

using log4net;
...
public partial class MainWindow
{
    ...
    private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    ...
    private void BtnChangeLocation_Click(object sender, RoutedEventArgs e)
    {
        ...
        Log.Debug("This is a Debug message");
        Log.Info("This is a Info message");
        Log.Warn("This is a Warning message");
        Log.Error("This is an Error message");
        Log.Fatal("This is a Fatal message");
    }
}

When I run the application and click on the button (BtnChangeLocation_Click) no file is created, or even if I create the file manually, nothing is inserted into it. What could be the problem?

I'd suggest to read this: Log4net Tutorial .

There you'll find 14 steps to do to properly install and configure log4Net NuGet package.

One of the most important steps to do is to add log4net.config file, then to add an entry to AssemblyInfo.cs file... Follow the link for further details.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM