Is it a good practice to have logger as a singleton?

后端 未结 8 933
迷失自我
迷失自我 2020-12-12 12:08

I had a habit to pass logger to constructor, like:

public class OrderService : IOrderService {
     public OrderService(ILogger logger) {
     }
}

相关标签:
8条回答
  • 2020-12-12 12:35

    I put a logger instance in my dependency injection container, which then injects the logger into the classes which need one.

    0 讨论(0)
  • 2020-12-12 12:40

    There is a book Dependency Injection in .NET. Based on what you need you should use interception.

    In this book there is a diagram helping to decide whether to use Constructor injection, property injection, method injection, Ambient Context, Interception.

    That's how one reasons using this diagram:

    1. Do you have dependency or need it? - Need it
    2. Is it cross-cutting concern? - Yes
    3. Do you need an answer from it? - No

    Use Interception

    0 讨论(0)
提交回复
热议问题