再次关于log4net和Unity IOC配置 [英] again about log4net and Unity IOC config

查看:320
本文介绍了再次关于log4net和Unity IOC配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到了几个这些问题,围绕各种网站,答案似乎是由l4n officianados转向log4net的轻量级封装的价值是'(你不是吗?)和类似(这是我的问题)是如何将log4net对象模型拟合到寄存器类型/数据库中的方法。在流程配置接口中的registerinstance序列。



这里的目标不是重新包装l4n,而是简单地抓住一个不错的参考,不需要劫持流利



差的ejemplow,我想从LogManger.GetLogger方法获得一个配置的ILog实例的引用,并把它放在流畅的流中



因此,为了回应一个不耐烦的建议,我尝试以规范的方式创建了ILog的正常实例:

  log4net.Config.XmlConfigurator.Configure(); 
static readonly log4Net.Ilog Log = LogManager.GetLogger(thatlongassemblyreflectionstuff);

因此,现在添加对Unity的引用似乎微不足道



但是,RegisterInstance方法的签名需要一个Type而不是一个接口。



对于那些没有搜索过log4net对象模型的人,afficiananderos是正确的:l4n是一个'wrapper',你不能得到一个实际的'type'for the Log thingie。



现在我必须测试。你知道这是什么意思,它可能需要一分钟,但更可能需要一个小时或四个(类似于'小时'和'四个'的拼写在现实生活中从来不是偶然的)。



但是,以下,减去规范设置的省略部分工作:

 容器
.RegisterInstance(Logger,Log,new ContainerControlledLifetimeManager())
.RegisterType< IControllerContext,ControllerContext>

CtlrCtx,
new ContainerControlledLifetimeManager(),
new InjectionConstructor(new ResolvedParameter< IMessageBuilder>(MsgBldr)),
new InjectionProperty ,new ResolvedParameter< ILog>(Logger))
);

因此,原始Log对象和Log对象的哈希码注入到我的对象是相同的。



但是...



注入过程需要我公开上下文对象通过其接口,意味着它可能不再是一个静态对象。而且log4net ILog对象是否是静态的似乎是决定因素,它是否是可串行化的,可以在组件之间mashalled没有戏剧性的运行时将变得不稳定的警告(这是真正有意义的只有Matrix风扇) p>

虽然不鼓励,但是我使用了Resharper的漂亮的to属性与一个支持字段,并将支持字段设置为静态,而Interface属性保持非静态。它构建和测试跑绿色。



所以我甚至做了一个重建,它的工作。所以也许当这个泡沫到集成测试,我会偷偷通过log4net是不可序列化的崩溃。



所以也许这将有助于



感谢



Stato

解决方案

在Unity 2中使用InjectionFactory有帮助吗? (请参阅此问题)。然后你的配置代码看起来像这样:

  IUnityContainer container = new UnityContainer 
container.RegisterType< ILog>(new InjectionFactory(factory => LogManager.GetLogger()));然后你通常使用Resolve()调用记录器:


b

  ILog logger = container.Resolve< ILog>(); 
logger.Log(Level.Debug,Hello world);

您也可以将记录器的生命周期配置为ContainerControllerLifetimeManager,一个单例实例,但我还没有验证。


I've bumped through a few of these questions laying around various sites and the answers seem to be spun by l4n officianados toward the value of the lightweight wrapper that log4net 'is' (don't you get it?) and similar points of mind-boggling fact.

However it seems that what users are asking for (and this is my question) is how to fit the log4net objet model into the registertype/registerinstance sequence in the fluent config interface.

The goal here wouldn't be to re-wrap l4n, but simply to grab a decent reference that doesnt' require hijacking the fluent flow, as it were.

poor ejemplow, I want to get a reference to a configured instance of ILog from a LogManger.GetLogger method and put it into the fluent flow early enough to inject it into properties of my downstream objects.

So in response to one impatient suggestion, I tried created a normal instance of ILog in the canonical manner:

log4net.Config.XmlConfigurator.Configure();
static readonly log4Net.Ilog Log = LogManager.GetLogger(thatlongassemblyreflectionstuff);

So it would seem trivial (in the real sense of effortless) to now add that reference to the Unity container and get on with my wonderful life.

However, the signature for the RegisterInstance method wants a 'Type' not an interface.

For those who haven't searched through the log4net object model, the afficiananderos are correct: l4n is a 'wrapper' and you can't get a holt of the actual 'type' for the Log thingie.

So now I have to test. And you know what that means, it could take a minute, but will more likely take an hour or four (similarilties like the spellings of 'hour' and 'four' are never coincidental in real life).

However, the following, minus the elided part about the canonical setup, did work:

container
  .RegisterInstance("Logger", Log, new ContainerControlledLifetimeManager())
.RegisterType<IControllerContext, ControllerContext>
(
   "CtlrCtx", 
   new ContainerControlledLifetimeManager(), 
   new InjectionConstructor(new ResolvedParameter<IMessageBuilder>("MsgBldr")),
   new InjectionProperty("Log",new ResolvedParameter<ILog>("Logger"))
);

So the hashcodes for the orginal Log object and the Log object injected into the property of my marvy little Context object are identical.

However...

The injection process required that I expose the Log property of the Context object through its interface, meaning it could no longer be a static object. And whether the log4net ILog object is static seems to be the deciding factor as to whether it is serializable and can be mashalled between assemblies without the dramatic 'the runtime will become unstable' warnings (which are truly meaningful only to Matrix fans).

Discouraged, though not deterred, I used Resharper's nifty 'to property with a backing field' and set the backing field to be static whilst the Interface property stayed non-static. Well it built and the test ran green.

So I even did a rebuild and it worked. So maybe when this bubbles up to the integration tests I'll sneak past the log4net isn't serializable debacle.

So maybe this will help

Thanks

Stato

解决方案

Would using the InjectionFactory in Unity 2 help? (see this question). Then your configuration code would look something like this:

IUnityContainer container = new UnityContainer();
container.RegisterType<ILog>(new InjectionFactory(factory => LogManager.GetLogger()));

Then you retrieve the logger with the usual call to Resolve():

ILog logger = container.Resolve<ILog>();
logger.Log(Level.Debug, "Hello world");

You might also be able to configure the lifetime of the logger to be ContainerControllerLifetimeManager as well, to make it a singleton instance, but I haven't verified that yet.

这篇关于再次关于log4net和Unity IOC配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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