我正在尝试将 Unity 配置为将 ILog 注入(inject)到我的类中,其中 LogManager.CreateLogger() 中的类型/名称设置为注入(inject) ILog 的类。
这是 similar question , 但对于结构图
最佳答案
我知道这个答案很晚,但我最近遇到了这个问题。
现在有一个 NuGet 包,UnityLog4NetExtension ,注册 log4net.ILog
界面和一些 Unity 构建策略,允许您只注入(inject) ILog
接口(interface)并让 Unity 使用它被注入(inject)的类来创建它。
该代码也可通过 GitHub 获得.
例如:
在需要注入(inject) ILog 实例之前,将扩展添加到统一容器。我已经在 Bootstrap 中完成了这个。
//add the extension to the unity container
container.AddNewExtension<Log4NetExtension>();
然后,只需注入(inject)
ILog
界面,它将工作public class MyClass
{
private readonly ILog _logger;
public MyClass(ILog loggerForMyClass)
{
_logger = loggerForMyClass;
}
}
这段代码就像我写了
_logger = LogManager.GetLogger(typeof(MyClass))
一样工作。
关于Log4net 和 Unity 注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6096376/