如何在 WPF 应用程序中使用 App.config 进行 log4net 配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25282076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use App.config in WPF application for log4net configuration
提问by ck84vi
Currently im working on a WPF version of an existing console application. In the console application i use log4net to do all my logging. Therefore i configured all my appenders etc in the App.config file. Everything works fine in the console application.
目前我正在研究现有控制台应用程序的 WPF 版本。在控制台应用程序中,我使用 log4net 来完成所有日志记录。因此,我在 App.config 文件中配置了所有 appender 等。在控制台应用程序中一切正常。
Now i want to implement the same logging functionality in my WPF application. I have to say that im totally new in WPF and this is my first WPF project. I just tried to add the App.config (exactly the same one) to my WPF project as i had it in my console application. But it does not work. No files are created by the FileAppenders. But i also dont get any error or warning when compiling.
现在我想在我的 WPF 应用程序中实现相同的日志记录功能。我不得不说我是 WPF 的新手,这是我的第一个 WPF 项目。我只是尝试将 App.config(完全相同)添加到我的 WPF 项目中,因为我在控制台应用程序中有它。但它不起作用。FileAppenders 不会创建任何文件。但是我在编译时也没有收到任何错误或警告。
What do i have to do to get the same logging functionality for log4net as in my console app? How can i configure log4net (Appenders) in an WPF application?
我该怎么做才能获得与控制台应用程序相同的 log4net 日志记录功能?如何在 WPF 应用程序中配置 log4net (Appenders)?
Thx in advance
提前谢谢
xxxxxx Edit xxxxxx
xxxxxx 编辑 xxxxxx
Based on Roberts hint i could solve it. I added
根据罗伯茨的提示,我可以解决它。我加了
log4net.Config.XmlConfigurator.Configure()
to my Main Window. Now my logging works exactly in the same way as it does in my console application.
到我的主窗口。现在,我的日志记录的工作方式与在控制台应用程序中的工作方式完全相同。
public MainWindow()
{
// check if Application is already running
// if it is running - Kill
if (System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Length > 1) System.Diagnostics.Process.GetCurrentProcess().Kill();
log4net.Config.XmlConfigurator.Configure();
InitializeComponent();
}
回答by Robert
You need to call log4net.Config.XmlConfigurator.Configure()on startup.
您需要log4net.Config.XmlConfigurator.Configure()在启动时调用。
回答by Basheer AL-MOMANI
its simple, inside Window Constructorjust add this line like this
它很简单,在里面Window Constructor只需像这样添加这一行
public MainWindow()
{
log4net.Config.XmlConfigurator.Configure();
InitializeComponent();
//.....
}
回答by Martin
That's the way you do it a bit more in detail
这就是你做的更详细的方式
using System.Windows;
using log4net;
namespace Namespace
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
protected override void OnStartup(StartupEventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
//Log.Info("Hello World");
base.OnStartup(e);
}
}
}
Don't forget to add log4net into the the configSections in your App.config
不要忘记将 log4net 添加到 App.config 的 configSections 中
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" />
</configSections>

