ASP.NET Core Web API Logging from a Static Class

后端 未结 4 547
夕颜
夕颜 2020-12-29 20:58

I\'m logging just fine using dependency injection on my controllers, now I need to log something from a static class.

How can I log from

4条回答
  •  悲&欢浪女
    2020-12-29 21:41

    First of all, I agree with NightOwl888 answer.

    But as an option (consider this as a workaround) you could inject ILogger dependency via method parameter => so a class that will call this static method should be responsible for providing a correct implementation of ILogger.

    static class YourClass
    {
        public static void DoSomething(ILogger logger)
        {
             // do something
             // call log.Log();
        }
    }
    

提交回复
热议问题