如何为 Unity 中的所有注册类型配置日志拦截器? [英] How to configure a logging interceptor for all registered type in Unity?
问题描述
我将跟踪由 Unity Container 管理的服务中发生的每个操作作为文件系统中的日志.我想我可以定义一个名为 IService 的接口,并且所有其他接口或实现都应该从他那里继承.另一方面,我想开发一个自定义拦截行为或调用处理程序来将日志保存到文件中.
I am going to trace every actions happened in the services which are managed by the Unity Container as logs in file system. I suppose I could define an interface named IService and all the other interfaces or implementation should inherit from him. On the other hand, I would like to develop a custom interception behavior or call handler to save the logs into files.
不幸的是,我发现使用此代码对我不起作用
Unfortunately, I found that it doesn't work for me by using this codes
IUnityContainer unity = new UnityContainer();
//Interception
unity.AddNewExtension<Interception>();
Interception interception = unity.Configure<Interception>();
unity.RegisterType<IService>(
new DefaultInterceptor(new InterfaceInterceptor()),
new DefaultInterceptionBehavior(new LoggingBehavior()));
string[] configFiles = Directory.GetFiles(".", "*.config");
foreach (string configFile in configFiles)
{
var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile };
System.Configuration.Configuration configuration =
ConfigurationManager.OpenMappedExeConfiguration(fileMap,
ConfigurationUserLevel.None);
var unitySection = (UnityConfigurationSection)
configuration.GetSection("unity");
unity = unitySection.Configure(unity);
}
IGateway imapGW = unity.Resolve<IGateway>("ImapGateway");
我在Unity中使用拦截器有什么误解吗?如何处理此问题以自动记录所有内容而无需为每个服务配置拦截器?
Is there any misunderstanding for me to use interceptor in Unity? How can I deal with this issue to log everything automatically without configuring interceptor for each service?
推荐答案
实现记录器的方法有很多种.很久以前写过一篇关于如何设置拦截器的文章:
There are many ways to implement the logger. Long time ago I wrote an article about how to set up interceptors:
对于日志记录,您可以使用 Debug.Write,并配置 TraceListeners.或者您也可以使用企业库或任何第三方日志库.
For logging you could use Debug.Write, and configure TraceListeners. Or also you could Use Enterprise Library or any third party log library.
亲切的问候,赫伯
这篇关于如何为 Unity 中的所有注册类型配置日志拦截器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!