2016-09-16 2 views
0

ASP.NET MVC 5에서 작업 중입니다. 컨트롤러의 액션에서 발생하는 모든 예외를 기록하려고합니다.클래스의 모든 메서드/액션에 애스펙트 적용

이 작업을 수행하려면 PostSharp (dll에서)를 사용하여 사용자 정의 측면을 생성합니다. 이미 로그 파일을 작성하는 코드를 작성 했으므로 이제는 컨트롤러 전체에서 사용할 수 있기를 원합니다. 모든 방법에 수작업으로 적용하고 싶습니다.)

는 측면의 코드는 다음과 같습니다

using System; 
using PostSharp.Aspects; 

namespace Banlinea.Ceb.Domain.Aspects 
{ 
    public class LogException : OnExceptionAspect 
    { 
     public LogException() 
     { 
      ApplyToStateMachine = true; 
     } 

     public override void OnException(MethodExecutionArgs args) 
     { 
      //Code for logging the exception 

      args.FlowBehavior = FlowBehavior.ThrowException; 
     } 
    } 
} 

을 지금 내 컨트롤러에서 내가 원하는 것은 같은 것을하는 것입니다 : 나는 그렇게 할 수있는 방법,

[LogException] 
public class MyController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    public ActionResult Other() 
    { 
     return View(); 
    } 

    public ActionResult Another() 
    { 
     return View(); 
    } 
} 

그냥 클래스를 장식을 ?

답변

0

당신은 attribute multicasting라는 기능을 사용하여이 byimplementing IAspectProvider

http://doc.postsharp.net/iaspectprovider

public IEnumerable<AspectInstance> ProvideAspects(object targetElement) 
{ 
    Type type = (Type)targetElement; 

    return type.GetMethods().Select( 
     m => return new AspectInstance(targetElement, new LogException())); 

} 
0

당신은 당신의 코드베이스를 통해 PostSharp 측면을 적용 할 수 있습니다 할 수 있습니다.

예를 들어 클래스 수준이나 어셈블리 수준에서 메서드 수준의 속성을 적용하면 해당 클래스 나 어셈블리의 모든 메서드에 자동으로 복사됩니다. AttributeTargetTypes, AttributeTargetMemberAttributes 등과 같이 대상 속성을 추가로 필터링 할 수 있습니다.

질문의 샘플 코드는 실제로 예상대로 작동해야합니다.