Reputation: 219117
I wonder if this is something very simple that I'm just missing, or if there's a lot more to this.
Basically, what I'm trying to do is use StructureMap's setter injection for a logger implementation. The constructor for the logger that I wish for StructureMap to use accepts a Type
parameter for creating the logger:
public class Logger : ILogger
{
private ILog _log = null;
public Logger() { _log = LogManager.GetCurrentClassLogger(); }
public Logger(string name) { _log = LogManager.GetLogger(name); }
public Logger(Type loggerType) { _log = LogManager.GetLogger(loggerType); }
// The rest of the implementation...
}
In my bootstrapping code, I initialize it as thus:
ObjectFactory.Configure(x => {
x.FillAllPropertiesOfType<ILogger>().Use(l =>
new Logger(l.BuildStack.Current.ConcreteType));
// Further unrelated bootstrapping...
});
It's not quite working 100%, and it's probably just a lack of full understanding on my part. So I have a few questions regarding the behavior I'm seeing:
ILogger
. How can I get it to be of the type which contains the setter being injected?Maybe there's an entirely different/simpler/better way to accomplish what I'm trying to do, and I welcome suggestions for that. Basically, I'm abstracting my logging implementation behind an IoC container, and I need class-specific loggers.
Upvotes: 0
Views: 144
Reputation: 172875
Perhaps this is not the exact answer you are looking for, but why not simplify everything by creating an ILoggerFactory
? Instead of registering the ILogger
, you can register a ILoggerFactory
and inject the ILoggerFactory
in your classes:
public interface ILoggerFactory
{
ILogger CreateFor(string name);
ILogger CreateFor<T>();
ILogger CreateFor(Type loggerType);
}
public class LoggerFactory : ILoggerFactory
{
public ILogger CreateFor(string name)
{
return LogManager.GetLogger(name);
}
public ILogger CreateFor<T>()
{
return CreateFor(typeof(T));
}
public ILogger CreateFor(Type loggerType)
{
return LogManager.GetLogger(loggerType);
}
}
Upvotes: 1