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.ILoggerProvider
interface (and related classes) which will allow you to assert that specific log output messages were written, or to redirect ILogger
and ILogger<T>
output to your test's own output.
ILogger
usage and/or if you don't want your SUT's own log output written to your test output, then use NullLoggerFactory
or NullLogger<T>.Instance
in your arrange section to create a stub ILogger<T>
that doesn't do anything.Like this:
using Microsoft.Extensions.Logging.Abstractions;
// [...]
[Fact]
public void MyService_should_do_something()
{
// Arrange:
IArbitraryDependency dep = new StubArbitraryDependency();
#if FULL_MOON_TONIGHT
ILogger<MyService> nullLogger = NullLoggerFactory.Instance.CreateLogger<MyService>();
#else
ILogger<MyService> nullLogger = NullLogger<MyService>.Instance();
#endif
MyService systemUnderTest = new MyService(
arbitraryDependency: dep,
logger : nullLogger
);
// Act:
systemUnderTest.DoSomething();
// Assert:
// [...]
}