I want to inject a Microsoft.Extensions.Logging logger into the constructor of a class using the Unity DI container:
using Microsoft.Extensions.Logging;
namespace LoggingExample
{
public class MyClass
{
private readonly ILogger<MyClass> _logger;
public MyClass(ILogger<MyClass> logger)
{
_logger = logger;
}
public void DoSomething()
{
_logger.LogInformation("Doing something.");
}
}
}
This Stackoverflow answer suggests it's a bad idea to inject ILogger<T> as it's too specific. Instead, the answer suggests injecting the non-generic base type, ILogger, which is simpler, easier to test and less error prone (from copy and paste errors mainly, I would guess).
In the class above we'd want to change the data type of the constructor parameter:
public MyClass(ILogger logger)
{
_logger = logger;
}
Then we'd need some way of resolving ILogger into Logger<MyClass> for this class, or Logger<T> in the more general case for any class, where T represents the class being injected into.
This is done in that Stackoverflow answer using the Simple Injector DI container and conditional registration:
container.RegisterConditional(
typeof(ILogger),
c => typeof(Logger<>).MakeGenericType(c.Consumer.ImplementationType),
Lifestyle.Singleton,
_ => true);
Is conditional registration possible in Unity? is there any other way Unity could recognize the class it's injecting ILogger into and resolve the reference to inject a Logger<T> object (where T is the class being injected into)?
Logger<MyClass>in your constructor. - howchengILoggertoLogger<T>when it is injectingILoggerobjects into the constructor, won't it? How will Unity know to use the appropriate type parameter when resolvingILoggerif there are multiple classesILoggeris being injected into? For example, in one class it might have to inject aLogger<MyClass>object, in a second class aLogger<SecondClass>object. It looks like Simple Injector can do that. Can Unity? - Simon Tewsi