How to test asp.net core built-in Ilogger

前端 未结 6 1443
无人共我
无人共我 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

    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(this Mock> mockLogger, Func times)
    {
        mockLogger.Verify(x => x.Log(
            It.IsAny(),
            It.IsAny(),
            It.Is((v, t) => true),
            It.IsAny(),
            It.Is>((v, t) => true)), times);
    }
    

提交回复
热议问题