top of page
Search
  • Writer's pictureAndrew Boyd

Don't use ILogger

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 colleagues in Support and even their future selves?


A concept I'm currently trialling 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<T>
{
    void CheckOutThisError(Exception exception, string message);
}

Then you simply create your wrapper object:

public class HeySupport<T> : IHeySupport<T>
{
    private readonly ILogger<T> _logger;

    public HeySupport(ILogger<T> 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 colleagues had a good laugh when they saw it and while I can't be sure it's helping them, I can say it's definitely 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.


UPDATE

I shared this blog with a friend, who also has many years experience, he said that while the principal to remind developers they need to consider what information will be helpful to support when your application is in production, it would be better off being made a part of the code review process. I agree with him and realise this could potentially cause some confusion, especially given there is nothing from stopping people from using ILogger, I've changed my processes accordingly, but it was worth a try.

7 views0 comments

Recent Posts

See All

Hosting a domain on WWW with Cloudflare

These instructions assume you have logged into Cloudflare and that you have set up your Domain & DNS records, and are on the Page Rules page. Get traffic off your naked domain (i.e. yourdomain.com) Se

Don't use Configuration!

One of the most used classes in .NET Core would have to be IConfiguration, found in the Microsoft.Extensions.Configuration Nuget package. You can access your configuration using Configuration["ASectio

Development Frustrations

I got to do some React work this week, which has been an educational experience, a lot went wrong that shouldn't have and I learnt a few lessons which I thought I'd share. Slow navigation I logged int

Comentarios


Post: Blog2_Post
bottom of page