Log4Net in the application object?

I start by logging in a working WPF application using Log4Net as a component of logging. Here is my question: in a simple desktop application, is there any reason not to create an instance of my registrar as a property of the App class (App.xaml.cs), like this?

public partial class App : Application
{
        private static readonly ILog p_Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public ILog Logger
        {
            get { return p_Logger; }
        }

        #endregion
    }
}

This will allow me to call the registrar

+5
source share
3 answers

One reason comes to mind: since the static constructor of the class Appis the first bit of your code to be run, you will create an instance ILogbefore you configure log4net. Therefore, the instance ILogwill not be used. Generally, you should do something like this:

public partial class App : Application
{
    private static ILog log;

    static App()
    {
        XmlConfigurator.Configure();
        log = LogManager.GetLogger(typeof(App));
    }
}

, MethodBase . typeof(App)? / , , ... typeof(App) ...

+7

, . , :

  • .
  • ( App).
+3

App. , , , , - .

GetLogger() , , , . , OpenFile :

// Get logger
var logger = LogManager.GetLogger("OpenFile"); 

, . log4net App(), . , :

2010-03-29 15:51:41,951 OpenFile [DEBUG]- Data file opened.

- , , , .

+2

All Articles