How to Use and Unit Test ILogger

July 3, 2020

ILogger is at the heart of the ASP.NET Core infrastructure and works well when you use it correctly. If you approach it the wrong way, it is easy to go down a rabbit hole and burn lots of time trying to implement basic functionality. Follow these tips instead. This article gives you the basics on how to use and verify mocked calls without having to implement a class yourself.

Here are some pointers in case you've been doing it wrong. Read on to understand the justification for these tips.

  • Only use ILogger or ILoggerFactory in your implementations. Avoid making direct API calls to the logging library
  • Use an off-the-shelf library. Don't create a class that implements ILogger unless you explicitly want to build your own logging infrastructure. 
  • Choose the library based on the logging infrastructure you choose to use. You will probably need to log to a cloud system. You need to compare and evaluate the functionality and cost with other systems before you go live
  • Log with the extension methods (LogInformation, LogWarning, and LogError). Don't use the ILogger's Log method directly. 
  • Use BeginScope with a message template and properties. You can also achieve the same by sending  Dictionary<string, object> to ensure that all log calls log the custom properties you want to show up in the logging system.
  • Use named string parameters. Don't use string interpolation for logging messages. 
  • Verify the output of the logging calls with a mocking framework such as Moq in your unit tests.
  • Read through the official documentation.

The Goal

Ultimately, the logger should write log messages to whichever system you choose to use. You will also want to write key/value pairs as part of the log messages so that you can later query the data. For example, you may want to mark a message with a "RecordType" property with a value of "Person". It would allow you to search for all messages where the "RecordType" is "Person". You will probably want to be able to search on things like record Ids. This type of query is invaluable for pinpointing information in a vast sea of logs when the system gets bigger.

Choosing the right logging system is outside the scope of this article, but you also need to ensure that you don't blow your budget on logging. Make sure that you take some time to choose the best place to send your logs.

Injecting an ILogger

For simple scenarios, inject an ILogger instance into your class via the constructor. It is usually best practice to pass the class's type as a generic argument on ILogger. This is an example from the official Microsoft documentation.

For more complex scenarios, you will need to create a separate ILogger for each category and inject ILoggerFactory into the class. Categories allow different levels of filtering. Here is an example using ILoggerFactory

Logging System

Start with file or console logging. Here is a bare minimum sample for an ASP.NET Core app with console logging:

Check out the full code example here.

You may use an off the shelf library like NLoglog4net, or serilog to get up and running quickly. Configure one of these libraries using their documentation to log directly to a text file. This allows you to see log output while you are building the app. As long as you use dependency injection as above, you can change the library at any point in time. You will not be locked in. 

The next decision is where to put your logs. Some logging libraries will support these systems out of the box. However, you may need to use the specific SDKs from these systems. Here are some of the logging systems from the larger players: 

Here are some things to consider:

  • How much will it cost?
  • What is the bandwidth transfer be like? I.e., will log messages send unnecessary data?
  • Is privacy an issue? Is the service GDPR compliant? Will the service log the IP address of users?
  • Is it easy to query logs? What's the syntax for querying like?
  • Is there a lag between logging the message and when it shows up in a query?

The most important thing here is testing. Build your application and regularly trial sending logs to candidate logging systems. Test that the system allows you to find and search for logs easily, and check price estimations. If your app is going to log a lot of data, the chances are that logging will get expensive. Try multiple systems and compare them. The cost will come back to bite you if you don't.

Using ILogger

Use the ILogger extension methods. You shouldn't directly use the main Log method on the ILogger interface. The extension methods take care of the plumbing for you, and you should focus on writing useful information to the logging system. Just call one of these:

  • LogCritical
  • LogDebug
  • LogError
  • LogInformation
  • LogTrace
  • LogWarning


_logger.LogInformation("Log message");

Message Templates and Properties

Message templates allow you to specify a message with templated information like named properties. The templating syntax can get quite sophisticated. Named properties are important for logging. If you use string interpolation for messages, the processing of the string will strip the name of the properties before the logger processes the message, and the name of the properties will be lost. As an example, named properties show up in Application Insights as custom dimensions which means you can query these by name.


var count = 1;
logger.LogInformation("Count: {count}", count);


var count = 1;
logger.LogInformation("Count: {count}", count);

If you want to attach properties to each log message, use BeginScope

using (logger.BeginScope("Correlation ID: {correlationID}", 123))

Console output:

Console output:
info: ILoggerSamples.LogTest[0]
=> Correlation ID: 123
info: ILoggerSamples.LogTest[0]
=> Correlation ID: 123

This would allow you query the logs by correlation Id on the other side.

Unit Testing ILogger

You should verify that your code is sending log messages. You don't need to prove that the message ends up on the log server. You only need to prove that the correct Log call occurs. Unit testing ILogger is a little tricky because of the Log method's structure on the ILogger interface, but it is possible with Moq. This verifies that a log call happened, that the severity is Information, and that the log only occurred once. 

Check out the full code example here.

But, we need a lot more than this to know that logging is correct. With this example, we can verify that the message gets sent, the original message is correct, and we can check which scoped arguments get sent. Here is a set of methods to help you Verify log messages.



Logging is critical, but it doesn't have to be difficult. ILogger is your friend and not your enemy. If you find yourself writing lots of code to get it working, something has gone wrong. Keep your config code in the startup of your app, and avoid it in your services. Above all, pay close attention to the logging system you are working with.

My photoChristian Findlay

I am a Flutter, .NET and Cloud Developer living in Melbourne, Australia. I've been developing software and mentoring teams for over twenty years.

Related Posts