0

Microsoft's Built-in DI Container can't create a non-generic ILogger instance as described in this post.

How can I configure the DI Container (I use Lamar) to create generic ILogger where T is the requesting class - just like described here:

Inject ILogger or ILogger, avoid injecting ILoggerFactory
Prefer injecting the ILogger interface and if necessary the ILogger interface. Ideally your class’ constructor requests an ILogger and the IoC framework creates an instance of an ILogger where T is the requesting class.

I read in several posts that Autofac or Simple Injector DI Containers can do this. But is it also possible to achieve with Lamar?

I can set Lamar ServiceRegistry like this:

registry.For<ILogger>().Use(c => c.GetInstance<ILoggerFactory>() .CreateLogger("NonGeneric"));

which works. But I would like to create a generic logger according to the type the logger is injected into.

0 Answers0