Search code examples
c#asp.net-mvcaoppostsharp

AOP Postsharp - Aspect attributes not working


I'm trying to simple AOP approach with MVC project. Installed postharp express edition and packets. I've got 2 project,first is aspect module and second is casual mvc project.

So i created a custom log attribute with aspect that postsharp provided

[Serializable]
 public class LogAttribute : OnMethodBoundaryAspect
    {
        public LogAttribute()
        {
        }
        public override void OnEntry(MethodExecutionArgs args)
        {                
            Log.Instance.Info(args.Arguments[0]);

            base.OnEntry(args);
        }

        public override void OnExit(MethodExecutionArgs args)
        {
            Log.Instance.Info(args.Arguments[0]);
            base.OnExit(args);
        }

        public override void OnException(MethodExecutionArgs args)
        {
            Log.Instance.Error(args.Exception,args.Method.Name);
            base.OnException(args);
        }
    }

and this is my homecontroller

[Log]
    public class HomeController : Controller
    {

        public HomeController()
        {
        }

        public ActionResult Index( )
        {
            Debug.WriteLine("start");            
            return View();
        }
}

Postsharp documents pretty clear but my attribute never hit any method. I tried various attribute usage and other things but still nothing happens. I also tried to inspect IL code of my controller with and without Log attribute. Actually it seems there is no additional code coming from attribute with postsharp. P.S. : Yes, i tried clean and rebuild (like millions times).


Solution

  • I figured it out after hard trying. When we want to use Postsharp on our project, we must add Postsharp VS extension. After installing postsharp extension, we must logon and check our postsharp policy to target our classes with postsharp attributes (or custom derived postsharp attributes). Postsharp provide 10 target class for express edition. After compile check out for output for postsharp process, if it succeed, postsharp inject IL code to our method that we marked with aspect attributes. If we did everything right, we can get sessions,users etc everything can be logged without any workaround also provide dead easy caching mechanism.

    Its all about postsharp VS Extension & Policy settings.