How to test asp.net core built-in Ilogger

2020-05-22 15:18发布

I want to verify some logs logged. I am using the asp.net core built-in ILogger, and inject it with the asp.net core built-in DI:

private readonly ILogger<InvoiceApi> _logger;

public InvoiceApi(ILogger<InvoiceApi> logger)
{
    _logger = logger;
}

then I use it like: _logger.LogError("error));

I tried to mock it (with moq) as usual by:

MockLogger = new Mock<ILogger<InvoiceApi>>();

and inject this in the service for test by:

new InvoiceApi(MockLogger.Object);

then tried verify:

MockLogger.Verify(m => m.LogError(It.Is<string>(s => s.Contains("CreateInvoiceFailed"))));

but it throw:

Invalid verify on a non-virtual (overridable in VB) member: m => m.LogError

So, how can I verify this logs logged?

4条回答
等我变得足够好
2楼-- · 2020-05-22 15:28

LogError is an extension method (static) not an instance method. You can't "directly" mock static methods (hence extension method) with a mocking framework therefore Moq is unable to mock and hence verify that method. I have seen suggestions online about adapting/wrapping the target interface and doing your mocks on that but that would mean rewrites if you have used the default ILogger throughout your code in many places. You would have to create 2 new types, one for the wrapper class and the other for the mockable interface.

查看更多
地球回转人心会变
3楼-- · 2020-05-22 15:31

I've written a short article showing a variety of approaches, including mocking the underlying Log() method as described in other answers here. The article includes a full GitHub repo with each of the different options. In the end, my recommendation is to use your own adapter rather than working directly with the ILogger type, if you need to be able to test that it's being called.

https://ardalis.com/testing-logging-in-aspnet-core

查看更多
ら.Afraid
4楼-- · 2020-05-22 15:33

As @Nkosi've already said, you can't mock an extension method. What you should mock, is the ILogger.Log method, which LogError calls into. It makes the verification code a bit clunky, but it should work:

MockLogger.Verify(
    m => m.Log(
        LogLevel.Error,
        It.IsAny<EventId>(),
        It.Is<FormattedLogValues>(v => v.ToString().Contains("CreateInvoiceFailed")),
        It.IsAny<Exception>(),
        It.IsAny<Func<object, Exception, string>>()
    )
);

(Not sure if this compiles, but you get the gist)

查看更多
孤傲高冷的网名
5楼-- · 2020-05-22 15:38

After some upgrades to .net core 3.1 FormattedLogValues become internal. We can not access it anymore. I made a extensions method with some changes. Some sample usage for extension method:

mockLogger.VerifyLog(Times.Once);
public static void VerifyLog<T>(this Mock<ILogger<T>> mockLogger, Func<Times> times)
{
    mockLogger.Verify(x => x.Log(
        It.IsAny<LogLevel>(),
        It.IsAny<EventId>(),
        It.Is<It.IsAnyType>((v, t) => true),
        It.IsAny<Exception>(),
        It.Is<Func<It.IsAnyType, Exception, string>>((v, t) => true)), times);
}
查看更多
登录 后发表回答