2013-08-20 3 views
0

유니티 예외 로깅에 대한 샘플 프로젝트가 필요합니다.Unity 3.0 및 예외 로깅

내 요구 사항은 반환 매개 변수가있는 클래스 특성을 클래스에 넣고 모든 작업을 통합하도록합니다. 나는이 방법은 CustomExceptionHandler 로그인 반환 -1

[CustomExceptionHandler(-1)] 
    public static int process(){ 

    throw new Exception("TEST"); 

    } 

답변

0

을 우선 할 원하는

같은, 당신은 유니티를 사용하여 정적 방법을 요격 할 수 없습니다.

Developer's Guide to Dependency Injection Using Unity을 살펴보십시오. 특히, Interception using Unity에있는 장. 가이드의 코드를 수정하고 조정하면 다음과 같이 될 수 있습니다.

class CustomExceptionHandler : ICallHandler 
{ 
    public IMethodReturn Invoke(IMethodInvocation input, 
    GetNextHandlerDelegate getNext) 
    { 
    WriteLog(String.Format("Invoking method {0} at {1}", 
     input.MethodBase, DateTime.Now.ToLongTimeString())); 

    // Invoke the next handler in the chain 
    var result = getNext().Invoke(input, getNext); 

    // After invoking the method on the original target 
    if (result.Exception != null) 
    { 
     // This could cause an exception if the Type is invalid 
     result.ReturnValue = -1; 
     result.Exception = null;  
    } 

    return result; 
    } 

    public int Order 
    { 
    get; 
    set; 
    } 
} 


class CustomExceptionHandlerAttribute : HandlerAttribute 
{ 
    private readonly int order; 

    public CustomExceptionHandlerAttribute(int order) 
    { 
    this.order = order; 
    } 

    public override ICallHandler CreateHandler(IUnityContainer container) 
    { 
    return new CustomExceptionHandler() { Order = order }; 
    } 
} 

class TenantStore : ITenantStore 
{ 
    [CustomExceptionHandler(1)] 
    public int Process() 
    { 
     throw new Exception("TEST"); 
    } 
} 

container.RegisterType<ITenantStore, TenantStore>(
    new InterceptionBehavior<PolicyInjectionBehavior>(), 
    new Interceptor<InterfaceInterceptor>()); 
+0

첫 번째로, Unity를 사용하여 정적 메소드를 인터셉트 할 수 없습니다. >>>> 이것은 좋지 않습니다. 어떻게 AOP를 구현할 수 있을지 모르겠다. 포스트 샤프와 같은 기능을 갖춘 무료 솔루션이 필요합니다. –

+1

[Afterthough] (http://github.com/vc3/Afterthought)을 볼 수 있습니다. 거기에 많은 무료 대안이 없습니다. –