加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

依赖注入 – 如何使用Unity 2.0注入Log4Net ILog实现

发布时间:2020-12-14 00:47:23 所属栏目:百科 来源:网络整理
导读:最终这与设置log4Net有关,但一般来说,问题不是具体记录. 通常我想知道的是在Microsoft Unity 2.0中如何做,相当于与Castle.Facilities.Logging.LoggingFacility相同的东西.也就是声明对记录器依赖的能力,并使记录器使用注入对象的类型进行初始化. 在测试的精
最终这与设置log4Net有关,但一般来说,问题不是具体记录.

通常我想知道的是在Microsoft Unity 2.0中如何做,相当于与Castle.Facilities.Logging.LoggingFacility相同的东西.也就是声明对记录器依赖的能力,并使记录器使用注入对象的类型进行初始化.

在测试的精神上值得一千字,这里是我需要的:

class Logger_IOC_Tests
{
    //[Test] 
    public void Logger_should_be_initialized_with_the_type_of_the_object_that_is_using_it()
    {
        var container = new UnityContainer();
        /* Configuration Magic probably involiving registering either 
            * a custom IDependencyResolverPolicy or BuilderStrategy
            * goes here...
            */
        container.RegisterType<LoggerUser>(new ContainerControlledLifetimeManager());

        var user = container.Resolve<LoggerUser>();

        Assert.True(user.Logger.GetUserType() == user.GetType());
    }
}

interface ILogger
{
    Type GetUserType();
}

class Logger : ILogger
{
    private readonly Type _type;

    public Logger(Type type)
    {
        _type = type;
    }

    public Type GetUserType()
    {
        return _type;
    }
}

class LoggerUser
{
    public readonly ILogger Logger;

    public LoggerUser(ILogger logger)
    {
        Logger = logger;
    }
}
我不知道这是你正在寻找的,但我在几个月前看到它,当我看到你的问题时被提醒.我没有使用Unity,所以我不能真正比较你在链接上发布的内容.希望对您有用:

http://davidkeaveny.blogspot.com/2011/03/unity-and-log4net.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读