【发布时间】:2020-11-17 21:34:49
【问题描述】:
我正在尝试在我的 WPF 应用程序中配置 log4net,但我很难做到。我在这里阅读了所有关于它的问题,但没有一个能解决我的问题。在下面找到代码。
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) 时,不会创建任何文件,或者即使我手动创建文件,也不会向其中插入任何内容。可能是什么问题?
【问题讨论】:
-
你能否尝试将“ConversionPattern”更新为
-
您确定可以写入
C:\Mylogs目录吗?有时 C 的根目录会受到保护,以防止非管理员程序试图将东西放在那里。如果您将C:\Mylogs\Installer.log更改为仅Installer.log,则在运行后检查您的调试目录中的该文件。我刚刚启动了一个全新的 WPF 应用程序,将您拥有的所有内容复制并粘贴到我的应用程序中,并且无需任何修改即可完美运行。 -
请阅读:Log4net Tutorial
-
我已经尝试像@MaciejLos 教程一样再次执行所有步骤,并且日志记录终于开始工作了。非常感谢,请创建一个答案以将问题标记为已解决并领取赏金。
-
@MaciejLos Bounty 将在 11 小时后提供。
标签: c# wpf logging nuget log4net