The views and opinions expressed on this site are mine and do not necessarily reflect those of my employer

Don't use ILogger

POSTED ON 23 July, 2020 BY ANDREW BOYD - 1 min read

In a project recently, I noticed while ILogger was being passed into API controllers, it wasn't being used as much as it should, so I questioned, What would make developers consider their collegues in Support and even their future selves?

A concept I'm currently trailing is creating an interface that is basically a wrapper for the ILogger interface found in .NET Core. I named it IHeySupport and as I require methods from the ILogger, I simply add them to the interface, keeping in line with YAGNI.

How do you set it up?

A very simple implementation would look like this:

public interface IHeySupport
    void CheckOutThisError(Exception exception, string message);

Then you simply create your wrapper object:

public class HeySupport : IHeySupport
    private readonly ILogger _logger;

    public HeySupport(ILogger logger)
    	_logger = logger;

    public void CheckOutThisError(Exception exception, string message)
    	_logger.LogError(exception, message);

To register this wrapper, you simply add the following line to your startup.cs:

services.AddTransient(typeof(IHeySupport<>), typeof(HeySupport<>));

And your off!

How is it working out?

I set this up about a month ago and whenever I see IHeySupport, I am reminded that I need to consider what information I should be logging, ILogger never had me considering it. My collegues had a good laugh when they saw it and while I can't be sure it's helping them, I can say it's definately helping me. Maybe my little bit of insanity will help you too, I'd love to hear your feedback if you give it a try.