.net Unity基于代码的拦截/日志记录配置

.net Unity基于代码的拦截/日志记录配置,.net,logging,unity-container,ioc-container,unity-interception,.net,Logging,Unity Container,Ioc Container,Unity Interception,我使用Unity作为IoC容器,到目前为止效果很好。现在,我想使用带有TypeMatchingRule和LogCallHandler的拦截来记录对接口IMyInterface的所有调用。我正在通过代码配置unity,但无法让日志工作。有人能给我举个简单的例子吗?我在文档中发现了一些小片段,但我无法为我的用例构建一个有效的配置。好像我错过了大局 首先,做一个行为。以下是一个例子: public class MyLoggerBehavior : IInterceptionBehavior {

我使用Unity作为IoC容器,到目前为止效果很好。现在,我想使用带有TypeMatchingRule和LogCallHandler的拦截来记录对接口IMyInterface的所有调用。我正在通过代码配置unity,但无法让日志工作。有人能给我举个简单的例子吗?我在文档中发现了一些小片段,但我无法为我的用例构建一个有效的配置。好像我错过了大局

首先,做一个行为。以下是一个例子:

public class MyLoggerBehavior : IInterceptionBehavior
  {
    public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
    {
      var returnValue = getNext()(input, getNext);

      if (returnValue.Exception != null)
      {
        Global.Logger.TraceException("Exception intercepted", returnValue.Exception);
      }
      else
      {
        Global.Logger.Trace("Method {0} returned {1}", input.MethodBase, returnValue.ReturnValue);
      }
      return returnValue;
    }

    public IEnumerable<Type> GetRequiredInterfaces()
    {
      return new Type[0];
    }

    public bool WillExecute
    {
      get { return Global.Logger.IsTraceEnabled; }
    }
  }
公共类MyLoggerBehavior:IInterceptionBehavior
{
公共IMethodReturn调用(IMethodInvoke输入,GetNextInterceptionBehaviorDelegate getNext)
{
var returnValue=getNext()(输入,getNext);
if(returnValue.Exception!=null)
{
Global.Logger.TraceException(“截获异常”,returnValue.Exception);
}
其他的
{
Global.Logger.Trace(“方法{0}返回{1}”,input.MethodBase,returnValue.returnValue);
}
返回值;
}
public IEnumerable GetRequiredInterface()
{
返回新类型[0];
}
公共场所将被执行
{
获取{return Global.Logger.IsTraceEnabled;}
}
}
然后,注册它:

Container
        .AddNewExtension<Interception>()
        .RegisterType<IDao, NhDao>(new Interceptor(new InterfaceInterceptor()),
                new InterceptionBehavior(new MyLoggerBehavior())
        );
容器
.AddNewExtension()
.RegisterType(新的拦截器(新的InterfaceInterceptor()),
新的拦截行为(新的MyLoggerBehavior())
);

它将跟踪记录器中的每个调用

如果要将添加调用处理程序添加到拦截注册中,则需要执行以下操作(我尝试使变量名不言自明):

var intp=m_singleInstance.Configure()
.SetInterceptorFor(typeof(typeToIntercept),
新的TransparentProxyInterceptor());
var policy=intp.AddPolicy(policyNameString);
policy.AddMatchingRule(
新注入构造函数(
新注入参数(typeof(typeToIntercept)))
.AddCallHandler(类型为(LogCallHandler),
新的ContainerControlled LifetimeManager());

我正在使用Enterprise Library 5.0附带的Unity版本。我发现这本《模式与实践指南》非常有用:这是我要寻找的方向,但我想使用Enterprise Library 5.0的日志块,它已经包含了所有必需的类。我的问题旨在通过使用内置类获得结果。
var intp = m_singleInstance.Configure<Interception>()
    .SetInterceptorFor(typeof(typeToIntercept), 
        new TransparentProxyInterceptor());

var policy = intp.AddPolicy(policyNameString);

policy.AddMatchingRule<TypeMatchingRule>(
    new InjectionConstructor(
    new InjectionParameter(typeof(typeToIntercept))))
    .AddCallHandler(typeof(LogCallHandler), 
        new ContainerControlledLifetimeManager());