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.
ความคิดเห็น