问题
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");
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?
回答1:
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-design-pattern-with-unity-2/
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
来源:https://stackoverflow.com/questions/11186918/how-to-configure-a-logging-interceptor-for-all-registered-type-in-unity