title: How to unit test with ILogger in ASP.NET Core
body: <p>This is my controller:</p>
  
  <pre><code>public class BlogController : Controller
  {
      private IDAO&lt;Blog&gt; _blogDAO;
      private readonly ILogger&lt;BlogController&gt; _logger;
  
      public BlogController(ILogger&lt;BlogController&gt; logger, IDAO&lt;Blog&gt; blogDAO)
      {
          this._blogDAO = blogDAO;
          this._logger = logger;
      }
      public IActionResult Index()
      {
          var blogs = this._blogDAO.GetMany();
          this._logger.LogInformation("Index page say hello", new object[0]);
          return View(blogs);
      }
  }
  </code></pre>
  
  <p>As you can see I have 2 dependencies, a <code>IDAO</code> and a <code>ILogger</code></p>
  
  <p>And this is my test class, I use xUnit to test and Moq to create mock and stub, I can mock <code>DAO</code> easy, but with the <code>ILogger</code> I don't know what to do so I just pass null and comment out the call to log in controller when run test. Is there a way to test but still keep the logger somehow ?</p>
  
  <pre><code>public class BlogControllerTest
  {
      [Fact]
      public void Index_ReturnAViewResult_WithAListOfBlog()
      {
          var mockRepo = new Mock&lt;IDAO&lt;Blog&gt;&gt;();
          mockRepo.Setup(repo =&gt; repo.GetMany(null)).Returns(GetListBlog());
          var controller = new BlogController(null,mockRepo.Object);
  
          var result = controller.Index();
  
          var viewResult = Assert.IsType&lt;ViewResult&gt;(result);
          var model = Assert.IsAssignableFrom&lt;IEnumerable&lt;Blog&gt;&gt;(viewResult.ViewData.Model);
          Assert.Equal(2, model.Count());
      }
  }
  </code></pre>