2014-02-21 37 views
1

即時通訊嘗試創建一個帶有Microsoft Unity和AOP的記錄器,但有些東西不起作用......我不明白..我只知道我不記錄任何東西。用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的構造函數?

回答

1

每當我使用Unity完成此操作時,我從特殊的HandlerAttribute類繼承了屬性,而不是更通用的Attribute類。這樣做將使您覆蓋CreateHandler方法,並返回您的處理程序的實例:

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

一旦你的,你應該能夠連線它在統一引導程序是這樣的:

IUnityContainer container = new UnityContainer(); 

container.AddNewExtension<Interception>(); 

container.Configure<Interception>().SetInterceptorFor<IInterfaceName>(new InterfaceInterceptor()); 

return container; 

我不知道web.config發生了什麼,但我沒有任何東西在我的這裏概述的過程工作正常。