Use unique features of a component while it's registered on a common interface

29 views Asked by At

Let me give an example using Serilog and Microsoft.Extensions.Logging. The application uses Serilog as its logging component, the loggers can be used as ILogger<T> by dependency injection. Serilog has a nice feature called message template that allows something like:

//_logger is an instance of ILogger<SomeClass> injected in the constructor

var position = new { Latitude = 25, Longitude = 134 };
var elapsedMs = 34;

_logger.LogInformation("Processed {@Position} in {Elapsed} ms", position, elapsedMs);

It's working fine. My question is: the purpose of using ILogger<T> is to decouple my code from Serilog so that I can switch to other logging components easily (though no one will do this because Serilog rocks!!!), using Serilog unique feature "message template" contradicts the original intention. So what's the best practice here? This is not only a question for logging, but also for other components.

0

There are 0 answers