Using Ninject to fill Log4Net Dependency

后端 未结 7 1206
天命终不由人
天命终不由人 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:17

    I do like the idea of wrapping the Log4Net in my own interfaces. I don't want to be dependent on Ninjects implementation, because to me that just means I take a dependency on Ninject throughout my application and I thought that was the exact opposite of what dependency injection is for. Decouple from third party services. So I took the original posters code but I changed the following code to make it work.

        private string GetParentTypeName(IContext context)
        {
            var res = context.Request.ParentRequest.ParentRequest.Service.FullName;
            return res.ToString();
        }
    

    I have to call ParentRequest.ParentRequest so that when I print the layout %logger it will print the class that calls the Log4Net log method instead of the Log4Net class of the method that called the Log method.

提交回复
热议问题