简体   繁体   English

log4net 没有记录任何内容,也没有创建文件

[英]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.我正在尝试在我的 WPF 应用程序中配置 log4net,但我正在努力这样做。 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 log4net NuGet 版本:2.0.8

AssemblyInfo.cs装配信息.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主窗口.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.当我运行应用程序并单击按钮 (BtnChangeLocation_Click) 时,不会创建任何文件,或者即使我手动创建文件,也不会插入任何内容。 What could be the problem?可能是什么问题呢?

I'd suggest to read this: Log4net Tutorial .我建议阅读: Log4net 教程

There you'll find 14 steps to do to properly install and configure log4Net NuGet package.在那里,您将找到正确安装和配置 log4Net NuGet package 的 14 个步骤。

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.最重要的步骤之一是添加log4net.config文件,然后在AssemblyInfo.cs文件中添加一个条目...点击链接了解更多详细信息。

Good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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