How to test asp.net core built-in Ilogger

前端 未结 6 1446
无人共我
无人共我 2021-02-02 06:36

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<         


        
6条回答
  •  别跟我提以往
    2021-02-02 07:08

    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

提交回复
热议问题