log4net 既不记录任何内容也不创建文件
log4net is not logging anything nor creating the file
我正在尝试在我的 WPF 应用程序中配置 log4net,但我很难做到。我在这里阅读了所有关于它的问题,但 none 解决了我的问题。找到下面的代码。
log4net NuGet 版本: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");
}
}
当我 运行 应用程序并单击按钮 (BtnChangeLocation_Click) 时,没有创建任何文件,或者即使我手动创建了文件,也没有向其中插入任何内容。可能是什么问题?
我建议阅读以下内容:Log4net Tutorial。
您会在此处找到正确安装和配置 log4Net NuGet 程序包的 14 个步骤。
最重要的步骤之一是添加 log4net.config
文件,然后向 AssemblyInfo.cs
文件添加条目...请按照 link 了解更多详细信息。
祝你好运!
我正在尝试在我的 WPF 应用程序中配置 log4net,但我很难做到。我在这里阅读了所有关于它的问题,但 none 解决了我的问题。找到下面的代码。
log4net NuGet 版本: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");
}
}
当我 运行 应用程序并单击按钮 (BtnChangeLocation_Click) 时,没有创建任何文件,或者即使我手动创建了文件,也没有向其中插入任何内容。可能是什么问题?
我建议阅读以下内容:Log4net Tutorial。
您会在此处找到正确安装和配置 log4Net NuGet 程序包的 14 个步骤。
最重要的步骤之一是添加 log4net.config
文件,然后向 AssemblyInfo.cs
文件添加条目...请按照 link 了解更多详细信息。
祝你好运!