Catel 3 -> logging with log4net

Apr 28, 2012 at 12:27 PM
Edited Apr 28, 2012 at 12:32 PM

Hi Geert,

How can I use Catel internal logging system with log4net? Here is my attempt:

1. I added reference to log4net in Catel.Examples.WPF.Validation project.

2.  I added [assembly: log4net.Config.XmlConfigurator(Watch = true)] to AssemblyInfo.cs

3.  I changed following attribute in app.config to:
<file value="Catel.Examples.WPF.Validation.log"/>

4.  I tried following code in MainWindowViewModel.cs:

 

   public class MainWindowViewModel : ViewModelBase

   {

       private static readonly ILog Log = LogManager.GetCurrentClassLogger();

 

       public MainWindowViewModel(): base()

       {

           Log.Error(new Exception("Exception text"), "My text");

       ..

but without success. Project run without compilation errors, but File Catel.Examples.WPF.Validation.log was not created.

When I tried following code:

   public class MainWindowViewModel : ViewModelBase

   {  

       public MainWindowViewModel(): base()

       {

              log4net.Config.BasicConfigurator.Configure();

           log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

           log.Error("My text", new Exception("Exception text"));

 

       ..

All work what I expected: File Catel.Examples.WPF.Validation.log was created and error was logged.

Can you explained what I missed, what I do wrong, please? Can you update for example Catel.Examples.WPF.Validation project in Catel examples? Thanks.

Martin.

Apr 29, 2012 at 2:08 PM

Cool, we were about to write documentation about this, so we can definitely use something in your post to use in the docs.

The only thing you need to do is write a custom listener and register it. We already wrote docs for NLog, which basically is the same.

1) Create a log listener that forwards all the logs to log4net

2) Register the log listener to the log manager

And all should be forwarded to your log4net logging.

Apr 30, 2012 at 7:00 AM

Hi Geert,

Great, works for me, thanks for reply.

I created class:

   public class log4netListener : LogListenerBase

   {              

       private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

 

       public override void Debug(ILog log, string message)

       {

           Log.Debug(message);

       }

 

       public override void Info(ILog log, string message)

       {

           Log.Info(message);

      }

 

       public override void Warning(ILog log, string message)

       {

           Log.Warn(message);

       }

 

       public override void Error(ILog log, string message)

       {

           Log.Error(message);

       }

   }

 

I added in App.xaml.cs :

   protected override void OnStartup(StartupEventArgs e)

       {

           var listener = new log4netListener();

           listener.IsDebugEnabled = false;

           listener.IsInfoEnabled = false;

           listener.IsWarningEnabled = false;

           listener.IsErrorEnabled = true;

           Catel.Logging.LogManager.AddListener(listener);

...

 

Is this best practice to use Catel internal logging system with log4net?

Thanks.

Martin

Apr 30, 2012 at 2:08 PM

Hi Geert,

I have other questions about Catel and logging.

In our project have third-party assembly for in Data Access Layer, that use internally use log4net. In Presentation Layer (WPF (Catel)) use Catel internal logging system with log4net described above. Listener is configured only for logging errors (see my post above), in log file I see only ERROR from Presentation Layer, but also DEBUG information from third-party assembly.

How can I configure listener or log4net globally for application?

How can I configure settings differently for different assemblies (different log file, level,..)?

Martin.

Apr 30, 2012 at 7:02 PM

Yes, it is the recommended way to use the Catel logging with log4net.

Log4net allows you to configure its behavior. You can check out this link for more info.