Using a DI / IoC or static method using log4net and getting the correct type and class method?

We are trying to wrap log4net inside some kind of shell, whether it be an injection class or a static wrapper method. We try not to announce this in every class requiring registration:

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

This PERFORMS approach is good, however, since it registers the correct type, as well as the method in which it was called.

The problem we are facing is the definition of a shell or some kind that would allow us to ignore this call - avoiding the ugly declarations for each class, while maintaining the necessary level of detail. In this case, when the .Debug method is called (a string message), we want all the actions of the method processing to allow the base log4net instance to write the correct type and method, as is usually the case.

In our tests, from static wrappers to injections of ILog instances, we were unable to get both pieces of information.

Is there a suitable working example, preferably using StructureMap, to connect this method, or at least to some method other than IoC that abstracts the setup of the log manager?

Thanks.

+4
2

StructureMap , , . ( "Injected", )

namespace EC2Utilities.Common.Factory
{
    public static class ContainerBootstrapper
    {
        public static void BootstrapStructureMap()
        {
            // Initialize the static ObjectFactory container
            ObjectFactory.Initialize(x =>
            {
                x.For<IBackupEngine>().Use<BackupEngine>();
                x.For<IConfigResourceAccess>().Use<ConfigResourceAccess>();
                x.For<IEc2ResourceAccess>().Use<Ec2ResourceAccess>();
                x.For<IScheduleEngine>().Use<ScheduleEngine>();
                x.For<IScheduleManager>().Use<ScheduleManager>();
                x.For<IBackupManager>().Use<BackupManager>();
                x.For<ILog>().Use(y => LogManager.GetLogger("Injected"));
                x.For<IInstanceManager>().Use<InstanceManager>();
                x.RegisterInterceptor(new ResourceAccessTypeInterceptor());
            });
        }
    }
}

% C.% M appender.layout.conversionPattern, , , ILog , ILog, :

namespace LoggingTest
{
    public delegate void LogFormat(string format, params object[] args);

    public interface ILoggerWrapper
    {
        Action<object> Debug { get; } 

        Action<object, Exception> DebugEx { get; }

        LogFormat DebugFormat { get; }


        Action<object> Info { get; }

        Action<object, Exception> InfoEx { get; }

        LogFormat InfoFormat { get; }


        Action<object> Warn { get; }

        Action<object, Exception> WarnEx { get; }

        LogFormat WarnFormat { get; }


        Action<object> Error { get; }

        Action<object, Exception> ErrorEx { get; }

        LogFormat ErrorFormat { get; }


        Action<object> Fatal { get; }

        Action<object, Exception> FatalEx { get; }

        LogFormat FatalFormat { get; }
    }
}

:

namespace LoggingTest
{
    public class LoggerWrapper : ILoggerWrapper
    {
        private readonly ILog _log;

        public LoggerWrapper(ILog log)
        {
            _log = log;
        }     

        public Action<object> Debug { get { return _log.Debug; } }

        public Action<object, Exception> DebugEx { get { return _log.Debug; } }

        public LogFormat DebugFormat { get { return _log.DebugFormat; } }

        public Action<object> Info { get { return _log.Info; } }

        public Action<object, Exception> InfoEx { get { return _log.Info; } }

        public LogFormat InfoFormat { get { return _log.InfoFormat; } }

        public Action<object> Warn { get { return _log.Warn; } }

        public Action<object, Exception> WarnEx { get { return _log.Warn; } }

        public LogFormat WarnFormat { get { return _log.WarnFormat; } }

        public Action<object> Error { get { return _log.Error; } }

        public Action<object, Exception> ErrorEx { get { return _log.Error; } }

        public LogFormat ErrorFormat { get { return _log.ErrorFormat; } }

        public Action<object> Fatal { get { return _log.Fatal; } }

        public Action<object, Exception> FatalEx { get { return _log.Fatal; } }

        public LogFormat FatalFormat { get { return _log.FatalFormat; } }
    }
}

, ILoggerWrapper .

,

+2

All Articles