I am trying to use log4net with Autofac. I've pasted this code http://autofac.readthedocs.org/en/latest/examples/log4net.html , and from Program.cs/Main() I am doing
var iocBuilder = new ContainerBuilder();
iocBuilder.RegisterModule(new LoggingModule());
var iocContainer = iocBuilder.Build();
now I would like to try this out immediately (in the next line), writing a simple line to the log file. How should I do it?
I am thinking of something like this
var ls = iocContainer.Resolve<LoggingModule>();
ls.Info("the logging is working");
Thanks a lot
To obtain a
ILog
, log4net need to know the class that use the logger (LogManager.GetLogger(...)
). So, you can't resolve aILog
without a parent class. You have to inject theILog
inside a component.In the application start (Global.asax.cs for Asp.net, MVC):
Another solution would be to register an
ILog
forObject
and resolveILog
. In this case, the module is not required.//In the DI container builder:
//In the function where we need to ue the logger: