Injecting an ILogger property with Autofac
autofac, IoC, programming 7 Comments »I’ve always liked the Castle.Core ILogger and ILoggerFactory abstraction. I don’t know why but I’ve never been a fan of log4net so I like to keep direct dependencies to it at arm’s length.
Looking into Autofac recently, I was trying to find a way of getting a logger property assigned as a component is created from the container in the same way as Windsor‘s logging facility. The Autofac container has a Module/IModule extensibility which works well for the tasks a facility would have done.
Here’s the type of code I’m used to seeing from logging.
using Castle.Core.Logging;
namespace LoggingStudy {
public interface IFoo {
void Bar();
}
public class Foo : IFoo {
public Foo() {
Logger = NullLogger.Instance;
}
public ILogger Logger { get; set; }
public void Bar() {
Logger.Info("Bar called");
}
}
}
Recent Comments