Log4Net XML config requires ILoggerRepository

Viewed 883

I am building a C# console app and I am using Autofac and log4net. I've followed Autofac's guide to add a log4net module, however I am having some difficulties configuring my logs. The problem is that when I try to create a XMLConfiguration, the compiler asks me to provide the ILoggerRepository and I don't know what that is. I've looked through the log4net documentation and the repository is not mentioned in this context.

Here is my module:

using System.Reflection;
using log4net;
using Autofac.Core;
using Autofac.Core.Registration;
using System.Linq;
using log4net.Config;
using System.IO;

namespace Logging
{
    public class LoggingModule : Autofac.Module
    {
        public LoggingModule()
        {
            // The error happens here!
            XmlConfigurator.Configure(new FileInfo("log4net.xml"));
        }

        private static void InjectLoggerProperties(object instance)
        {
            var instanceType = instance.GetType();

            // Get all the injectable properties to set.
            // If you wanted to ensure the properties were only UNSET properties,
            // here's where you'd do it.
            var properties = instanceType
              .GetProperties(BindingFlags.Public | BindingFlags.Instance)
              .Where(p => p.PropertyType == typeof(ILog) && p.CanWrite && p.GetIndexParameters().Length == 0);

            // Set the properties located.
            foreach (var propToSet in properties)
            {
                propToSet.SetValue(instance, LogManager.GetLogger(instanceType), null);
            }
        }

        private static void OnComponentPreparing(object sender, PreparingEventArgs e)
        {
            e.Parameters = e.Parameters.Union(
              new[]
              {
        new ResolvedParameter(
            (p, i) => p.ParameterType == typeof(ILog),
            (p, i) => LogManager.GetLogger(p.Member.DeclaringType)
        ),
              });
        }

        protected override void AttachToComponentRegistration(IComponentRegistryBuilder componentRegistryBuilder, IComponentRegistration registration)
        {
            // Handle constructor parameters.
            registration.Preparing += OnComponentPreparing;

            // Handle properties.
            registration.Activated += (sender, e) => InjectLoggerProperties(e.Instance);
        }
    }
}

The compile error happens here: XmlConfigurator.Configure(new FileInfo("log4net.xml")); and it says:

Severity    Code    Description Project File    Line    Suppression State
Error   CS1503  Argument 1: cannot convert from 'System.IO.FileInfo' to 'log4net.Repository.ILoggerRepository'  MyApp   C:\Dev\MyApp\Logging\LoggingModule.cs   15  Active
1 Answers

I've managed to solve my problem with the answer from this question.

Here is how I added the ILoggerRepository and added to the configuration:

ILoggerRepository repository = log4net.LogManager.GetRepository(Assembly.GetCallingAssembly());
log4net.Config.XmlConfigurator.Configure(repository, new FileInfo("log4net.xml"));

And here is my full working code:

using System.Reflection;
using log4net;
using Autofac.Core;
using Autofac.Core.Registration;
using System.Linq;
using log4net.Config;
using System.IO;
using log4net.Repository;

namespace Logging
{
    public class LoggingModule : Autofac.Module
    {
        public LoggingModule()
        {
            ILoggerRepository repository = LogManager.GetRepository(Assembly.GetCallingAssembly());
            XmlConfigurator.Configure(repository, new FileInfo("log4net.xml"));
        }

        private static void InjectLoggerProperties(object instance)
        {
            var instanceType = instance.GetType();

            // Get all the injectable properties to set.
            // If you wanted to ensure the properties were only UNSET properties,
            // here's where you'd do it.
            var properties = instanceType
              .GetProperties(BindingFlags.Public | BindingFlags.Instance)
              .Where(p => p.PropertyType == typeof(ILog) && p.CanWrite && p.GetIndexParameters().Length == 0);

            // Set the properties located.
            foreach (var propToSet in properties)
            {
                propToSet.SetValue(instance, LogManager.GetLogger(instanceType), null);
            }
        }

        private static void OnComponentPreparing(object sender, PreparingEventArgs e)
        {
            e.Parameters = e.Parameters.Union(
              new[]
              {
        new ResolvedParameter(
            (p, i) => p.ParameterType == typeof(ILog),
            (p, i) => LogManager.GetLogger(p.Member.DeclaringType)
        ),
              });
        }

        protected override void AttachToComponentRegistration(IComponentRegistryBuilder componentRegistryBuilder, IComponentRegistration registration)
        {
            // Handle constructor parameters.
            registration.Preparing += OnComponentPreparing;

            // Handle properties.
            registration.Activated += (sender, e) => InjectLoggerProperties(e.Instance);
        }
    }
}
Related