Which pattern to use for logging? Dependency Injection or Service Locator?

后端 未结 9 948
别那么骄傲
别那么骄傲 2021-01-31 15:51

Consider this scenario. I have some business logic that now and then will be required to write to a log.

interface ILogger
{
    void Log(string stuff);
}

inte         


        
9条回答
  •  离开以前
    2021-01-31 16:19

    Maybe this will be little offtopic, but why do we need injecting logger at all, when we can just type at the beggining of the class:

    Logger logger = LogManager.GetLogger("MyClassName");
    

    Logger doesn't change during development and later during maintenance. Modern loggers are highly customizable, so argument

    what if I want to replace text logger with database?

    is missed.

    I don't negate using dependency injection, I'm just curious about your mind.

提交回复
热议问题