Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
    }
like image 306
ck84vi Avatar asked Aug 13 '14 08:08

ck84vi


2 Answers

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

like image 104
Robert Avatar answered Nov 09 '22 12:11

Robert


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

public MainWindow()
{       
    log4net.Config.XmlConfigurator.Configure(); 
    InitializeComponent();
    //.....
}
like image 42
Basheer AL-MOMANI Avatar answered Nov 09 '22 13:11

Basheer AL-MOMANI