如何使用 Unity 2.0 注入 Log4Net ILog 实现 [英] How to Inject Log4Net ILog implementations using Unity 2.0

查看:37
本文介绍了如何使用 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 实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆