-
Notifications
You must be signed in to change notification settings - Fork 245
CreateLogger<T>() should return ILogger<T> #312
Comments
This makes sense. @davidfowl what do you think? @sunsided Would you be interested in sending a PR? |
@muratg I am, will do. 👍 |
@sunsided Thanks! |
@lodejard as well. I have a feeling there was some reason for this but I don't remember what it is. Does it have to do with some casting issues that happen in consumers of this or something? |
@Eilon |
Yeah but I feel like there was a reverse substitutability problem or something. Or I could be completely mistaken. Like now that you force taking an |
@lodejard Any thoughts on this one? |
I don't recall any of that. I wasn't involved with the design of |
There's no reason, it's just that |
Since
ILogger<T>
already is anILogger
, shouldn'tLoggerFactoryExtensions.CreateLogger<T>()
return anILogger<T>
instead of anILogger
?In order to automatically inject a logger, it needs to be of type
ILogger<T>
. If one needs to create an instance of a class with such a type in the constructor manually (e.g. in a unit test),ILoggerFactory
is required (either way, since we can't constructLogger<T>
without one).However, no method or extension of that class is able to construct the required type
ILogger<T>
by itself. Instead, the type has to be created manually withnew Logger<T>(factory)
which is kind of awkward, especially since it internally callsfactory.CreateLogger<T>()
- that is, I'm now passing a logger factory to a logger I created in order to construct itself ...The sad thing is that the neither of these perfectly valid looking options work
where the second one explodes with a firework at runtime.
My suggestion would be to implement
CreateLogger<T>()
asand the constructor of
Logger<T>
asinstead.
The text was updated successfully, but these errors were encountered: