【问题标题】:Create a Log with Unity and AOP使用 Unity 和 AOP 创建日志
【发布时间】:2014-03-01 06:34:56
【问题描述】:

我试图用 Microsoft Unity 和 AOP 创建一个记录器,但有些东西不起作用……我不明白什么……我只知道我没有记录任何东西。

这里是记录器的代码:

public class LoggerHandler : ICallHandler
{
    public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        //The code to write a file 

        return result;
    }

    ...
}

我在这里创建属性:

//create an attribute so you can apply it to your methods  
public class LoggerAttribute : Attribute
{
    public LoggerAttribute()
    {
         //Here I have a breakpoint just to understand if i go here. I never stop here
    }
}

这里是通过 web.config 配置拦截器的助手

public class GetTypeConverter : TypeConverter
{
    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        return Type.GetType(value.ToString());
    }
}

这里是 web.config

<types>                    
     <type type="en.MFS.BLL.Countries.ICountries, en.MFS.BLL.Countries"
                      mapTo="en.MFS.BLL.France.FranceBLL, en.MFS.BLL.France">

         <interceptor type="TransparentProxyInterceptor" />
     </type>
</types>

<extension type="Interception" />
<interception>
    <policy name="LoggerPolicy">
         <matchingRule name="TransactionMatchingRule" type="CustomAttributeMatchingRule">
             <constructor>
                  <param name="attributeType" type="System.Type">
                    <value value="en.MFS.LoggerInterception.LoggerAttribute"                               typeConverter="en.MFS.LoggerInterception.GetTypeConverter, en.MFS.LoggerInterception"/>
                  </param>
                  <param name="inherited" value="true" />
             </constructor>
        </matchingRule>
        <callHandler name="LoggerHandler"
                          type="en.MFS.LoggerInterception.LoggerHandler, en.MFS.LoggerInterception" >
                <property name="Order" value="1" />
        </callHandler>
     </policy>
</interception>

最后我是如何使用它的

[Logger]
GetCalculation_OutDTO GetCalculation(GetCalculation_InDTO calculationDTO);

那么,怎么了?为什么我没有设法进入 LoggerAttribute 的构造函数?

【问题讨论】:

    标签: c# logging unity-container aop


    【解决方案1】:

    每当我使用 Unity 完成此操作时,我都会从特殊的 HandlerAttribute 类而不是更通用的 Attribute 类继承属性。这样做将允许您覆盖 CreateHandler 方法并返回处理程序的实例:

    public class LoggerAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new LoggerHandler();
        }
    }
    

    一旦你有了它,你应该可以像这样在 Unity 引导程序中连接它:

    IUnityContainer container = new UnityContainer();
    
    container.AddNewExtension<Interception>();
    
    container.Configure<Interception>().SetInterceptorFor<IInterfaceName>(new InterfaceInterceptor());
    
    return container;
    

    我不确定 web.config 发生了什么,但我没有任何内容,此处概述的过程运行良好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      • 2012-11-04
      • 2012-09-30
      • 1970-01-01
      • 2018-04-13
      • 1970-01-01
      相关资源
      最近更新 更多