Should logging infrastructure be injected when using IoC/DI if logging facade is used?

前端 未结 5 491
栀梦
栀梦 2021-02-02 07:30

I am using Autofac as my IoC and from everything I have read on topic of DI teaches to use \"constructor injection\" to explicitly expose class dependencies... However, I am als

5条回答
  •  面向向阳花
    2021-02-02 07:45

    Logging is just infrastructure. Injecting it is overkill. I personally don't even use an abstraction layer. I use the static classes that the library provides directly. My motivation is that it's unlikely that I'll switch logging library in a current project (but might switch for the next project).

    However, you are using controllers in your example. Why do you need to log in them? A controller is just an adapter between the view and the model (business logic). There should be no need to log in it.

    You typically do only log in classes which contains business logic and at the top layer to be able to log unhandled exceptions. Those are the hard cases to debug and therefore the places where logging is required.

    Having to log at other places indicates that you need to refactor to encapsulate your business logic properly.

提交回复
热议问题