I'm trying to add logging to methods decorated with an attribute using Spring.Net for AOP.
Step 1: Reference 'Spring.Core', 'Spring.Aop', 'Common.Logging'
Step 2: Create an advice:
using AopAlliance.Intercept;
namespace MyApp.Aspects
{
public class LoggingAdvice : IMethodInterceptor
{
public object Invoke(IMethodInvocation invocation)
{
//todo: log started
object rval = invocation.Proceed();
return rval;
//todo: log finished
}
}
}
Step 3: Create an attribute:
using System;
namespace MyApp.Aspects
{
public class LoggingAttribute : Attribute
{
}
}
Step 4: Edit web.config
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springfrmework.net">
<object id="loggingAdvice" type="MyApp.Aspects.LoggingAdvice, MyApp"></object>
<object id="loggingAdvisor" type="Spring.Aop.Support.DefaultPointcutAdvisor, Spring.Aop">
<property name="Advice" ref="loggingAdvice" />
</object>
<object type="Spring.Aop.Framework.AutoProxy.AttributeAutoProxyCreator, Spring.Aop">
<property name="AttributeTypes" value="MyApp.Aspects.LoggingAttribute"/>
<property name="InterceptorNames" value="loggingAdvisor"/>
</object>
</objects>
</spring>
</configuration>
Step 5: Decorate a method with the attribute:
using System.Web.Mvc;
namespace MyApp.Controllers
{
public class MyController : Controller
{
[Logging]
public ActionResult DoStuff()
{
//todo: implement
}
}
}
The advice is never triggered. What am I missing?
See Question&Answers more detail:os