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

最终这与设置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

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...