Should I take ILogger, ILogger, ILoggerFactory or ILoggerProvider for a library?

后端 未结 8 1488
不知归路
不知归路 2020-12-04 06:59

This may be somewhat related to Pass ILogger or ILoggerFactory to constructors in AspNet Core?, however this is specifically about Library Design, not about

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 07:48

    This is only possible because ILogger is covariant and is just a wrapperclass that has a dependency on the LoggerFactory. If it wasn't covariant you would definitely be using the ILoggerFactory or ILogger. But ILogger should be discarded because you could be logging to any category and you would loose all context regarding the logging. I think ILoggerFactory would be the best way to go and then use CreateLogger() to create a ILogger inside you class. This way you really have a nice solution because as a developer you would really like to align the category with your classes to jump right to the code and not to some unrelated derived class. (You could add linesnumbers.) You can also let your derived classes use the logger that is defined by the baseclass but then also where to start looking for the source code? Besides this I can imagine you might also have any other ILogger with special purpose category (sub)names in the same class. Nothing is preventing you from have multiple ILogger's in such a case ILoggerFactory just looks cleaner.

提交回复
热议问题