Regarding using StructureMap to create your logger on the fly this is how I did it awhile back. (note 'Injected', I explain that next)
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());
});
}
}
}
With regards to keeping the goodness of using %C.%M in the appender.layout.conversionPattern to get your class and name, the trick is to expose the ILog methods as delegates in the wrapper so the calling class is actually calling the method in ILog rather than your wrapper:
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; }
}
}
And the implementation:
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; } }
}
}
You would of course also need to register ILoggerWrapper in the container as well.
HTH,
Eric