How to test asp.net core built-in Ilogger

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

    I have to do a fair bit of logger verification for black box processes at work and, after putting up with the expressions for a short period, I caved and put together an expression builder - Moq.Contrib.ExpressionBuilders.Logging.

    The end result is a shorter, fluent, verification statement:

    logger.Verify(
      Log.With.LogLevel(LogLevel.Error)
        .And.EventId(666)
        .And.LogMessage("I am a meat popsicle"), 
      Times.Once);
    

提交回复
热议问题