I have mostly used Ninject, so I apologize if I mix up terminology.
I created a logger module to handle NLog using the destination class name as the logger name. It is very similar to this: http://docs.autofac.org/en/latest/examples/log4net.html?highlight=log4net
I also have a module created in my service layer that takes care of all of the service level registrations. Looks like this:
public class ServiceModule : Module
protected override void Load(ContainerBuilder builder)
builder.Register(x => new AccountService(x.Resolve<ILogger>()))
var builder = new ContainerBuilder();
var container = builder.Build();
A module can do many things : it can register new components and/or subscribe to Autofac events.
In this case, the
LoggingModule doesn't register
ILog. It intercepts the preparation of other components using the
Preparing event and add a new
Parameter that will create the
ILog if needed.
You won't be able to resolve
ILog but if your
AccountService implementation requires a
ILog the following
Parameter will be triggered and will provide the
new ResolvedParameter( (p, i) => p.ParameterType == typeof(ILog), (p, i) => LogManager.GetLogger(p.Member.DeclaringType) )
All you have to do is to register your type without trying to explicitly create the instance :
builder.RegisterType<AccountService>() .As<IAccountService>() .InstancePerRequest();