如何配置为团结所有注册类型的日志记录拦截器? [英] How to configure a logging interceptor for all registered type in Unity?

查看:187
本文介绍了如何配置为团结所有注册类型的日志记录拦截器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要跟踪每个行动,这是由统一容器管理的文件系统日志服务发生了。我想我可以定义一个名为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:

http://hmadrigal.wordpress.com/2010/12/25/aspect-oriented-programming-and -interceptor设计图案与 - 团结-2 /

有关记录,你可以使用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.

亲切的问候,
数量锐减

Kind regards, Herber

这篇关于如何配置为团结所有注册类型的日志记录拦截器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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