How to use App.config in WPF application for log4net configuration
Asked Answered
P

3

8

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.

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.

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?

Thx in advance

xxxxxx Edit 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();
    }
Pension answered 13/8, 2014 at 8:58 Comment(3)
do you call log4net.Config.XmlConfigurator.Configure() on startup?Menticide
I've added the answer so that solution is clearly visibleMenticide
I have called log4net.Config.XmlConfigurator.Configure() but its not working. Please help me .. you have any log4net.config file in your project @PensionBassinet
M
10

You need to call log4net.Config.XmlConfigurator.Configure() on startup.

Menticide answered 13/8, 2014 at 9:17 Comment(0)
A
2

its simple, inside Window Constructor just add this line like this

public MainWindow()
{       
    log4net.Config.XmlConfigurator.Configure(); 
    InitializeComponent();
    //.....
}
Atop answered 10/7, 2016 at 10:52 Comment(0)
S
1

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

<configSections>
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" />
</configSections>
Severin answered 20/6, 2018 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.