本文介绍了如何使用Unity 2.0注入Log4Net ILog实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最终,这与设置log4Net有关,但一般而言,该问题并非特定于日志记录.

Ultimately this has to do with setting up log4Net but generically the problem is not logging specific.

通常,我想弄清楚的是在Microsoft Unity 2.0中该怎么做,这等同于使用Castle.Facilities.Logging.LoggingFacility获得的功能.也就是说,可以声明对记录器的依赖性,并使记录器初始化为要插入记录器的对象的类型.

Generically what I am trying to figure out is how to do, in Microsoft Unity 2.0, something equivalent to what one gets with the Castle.Facilities.Logging.LoggingFacility. Namely the ability to declare a dependency on a logger and have the logger initialized with the Type of the object into which it is being injected.

一个测试的精神值得一千个单词,这就是我所需要的:

In the spirit of a test is worth a thousand words, here is what I need:

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,所以我无法真正比​​较您发布的内容和链接中的内容.希望它将对您有用:

I don't know if this what you are looking for, but I saw it a few months ago and was reminded of it when I saw your question. I have not used Unity, so I can't really compare what you have posted with what is at the link. Hopefully it will be useful to you:

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

这篇关于如何使用Unity 2.0注入Log4Net ILog实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 00:59