I have my registration and constructor setup to use logging via Microsoft.Extensions.Logging as follows,
public MyService(ILogger<MyService> logger) {}
What I'd like to do, though, is use services.AddSingleton() in my Startup.cs to avoid using constructor injection. The reason is that it will otherwise require modifying all unit tests to Mock the logger when testing the service.
I believe there's a way to register the specific instance of ILogger in the ServiceCollection, but I cannot sort out the syntax required.
Thanks!
Microsoft.Extensions.Logging.ILoggerProviderinterface (and related classes) which will allow you to assert that specific log output messages were written, or to redirectILoggerandILogger<T>output to your test's own output.ILoggerusage and/or if you don't want your SUT's own log output written to your test output, then useNullLoggerFactoryorNullLogger<T>.Instancein your arrange section to create a stubILogger<T>that doesn't do anything.Like this: