Using Ninject to fill Log4Net Dependency

后端 未结 7 1209
天命终不由人
天命终不由人 2020-12-23 21:28

I use Ninject as a DI Container in my application. In order to loosely couple to my logging library, I use an interface like this:

public interface ILogger
         


        
7条回答
  •  生来不讨喜
    2020-12-23 22:23

    For all of you that are still looking for the correct answer, the correct implementation is :

    public class LoggingModule : NinjectModule
    {
        public override void Load()
        {
            Bind().ToMethod(x => LogManager.GetLogger(x.Request.Target.Member.DeclaringType));
    
            Bind().To()
                .InSingletonScope();
        }
    }
    

    Emphasis on:

    x.Request.Target.Member.DeclaringType
    

提交回复
热议问题