Description
We use Serilog for many years. In older projects we used it directly, creating a logger in each constructor by calling Log.ForContext<MyClass>()
. In newer projects we tried to use Microsoft logging abstractions which is at the end very similar, but it has one interesting feature which is either missing in Serilog or we don't know about it. So I would like to ask you to implement it natively in Serilog.
Microsoft logging abstractions contain generic interface ILogger<T>
so we can add a constructor dependency to ILogger<MyClass>
and it creates a new logger each time constructor is called. (I don't know how is it implemented, because it is neither singleton, nor transient, nor per-request, but it seems to work as expected. Our applications are ASP.NET services. We defined a factory method and registered it in Microsoft logging infrastructure, so it calls Log.ForContext<T>
behind the curtain.)
As a workaround, we can still manually call the above-mentioned Log.ForContext<ClassName>()
in each constructor, but some team members complain about this being a not-clean and repeated code since this should rather be another constructor dependency like any other.
Some team members tend to use Microsoft abstractions just to get the ILogger<T>
feature, but then we cannot use Serilog Analyzer nuget which we would like to use.
So another workaround would be to create our own implementation of ILogger<T>
for Serilog. But that leads me to a question why it isn't already implemented in Serilog itself? Or is it already somewhere?