I have the following Code.
public class TypeA : InterfaceA
{
private ILogger _logger;
public TypeA(ILogger logger)
{
_logger = logger;
}
}
public class Main
{
...
var loggerFactory = new LoggerFactory();
ILogger<TypeA> typeALogger = _loggerFactory.CreateLogger<TypeA>();
var typeAObj = new TypeA(typeALogger);
...
}
I want to use autofac to create the instance of TypeA like so:
// Autofac is configured ...
using var scope = container.BeginLifetimeScope();
var typeAObj = scope.Resolve<InterfaceA>();
How can I do this, but using the ILogger<TypeA> Logger from the existing LoggerFactory, as argument for the constructor?
If you want to reuse the existing Microsoft logging infrastructure you can reuse the build-in DI setup via
AutofacRegistration.PopulatefromAutofac.Extensions.DependencyInjection(a bit of docs):And registration: