Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring.net Logging Example using aop
    text
    copied!<p>I'm learning Spring.Net and am trying something simple, which is not working. I want to log any method calls decorated with <code>LogCall</code> </p> <pre class="lang-cs prettyprint-override"><code>namespace WpfApplication1 { public partial class MainWindow : Window { public MainWindow() { Test(); InitializeComponent(); } [LogCall] public void Test() { } } public class LogCallInterceptor : IMethodBeforeAdvice { public void Before(MethodInfo method, object[] args, object target) { Debug.Write(method.Name); } } [Serializable] [AttributeUsage(AttributeTargets.Method)] public class LogCallAttribute : Attribute { } } </code></pre> <p>And here's the App.config</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;configuration&gt; &lt;spring&gt; &lt;objects xmlns="http://www.springframework.net"&gt; &lt;object id="TestLogAdvice" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop"&gt; &lt;property name="advice"&gt; &lt;object type="WpfApplication1.LogCallInterceptor, WpfApplication1" /&gt; &lt;/property&gt; &lt;property name="attribute" value="WpfApplication1.LogCallAttribute, WpfApplication1" /&gt; &lt;/object&gt; &lt;/objects&gt; &lt;/spring&gt; &lt;runtime&gt; &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt; &lt;dependentAssembly&gt; &lt;assemblyIdentity name="Spring.Core" publicKeyToken="65e474d141e25e07" culture="neutral" /&gt; &lt;bindingRedirect oldVersion="0.0.0.0-1.3.2.40943" newVersion="1.3.2.40943" /&gt; &lt;/dependentAssembly&gt; &lt;dependentAssembly&gt; &lt;assemblyIdentity name="Spring.Aop" publicKeyToken="65e474d141e25e07" culture="neutral" /&gt; &lt;bindingRedirect oldVersion="0.0.0.0-1.3.2.40943" newVersion="1.3.2.40943" /&gt; &lt;/dependentAssembly&gt; &lt;/assemblyBinding&gt; &lt;/runtime&gt; &lt;/configuration&gt; </code></pre> <p>I'm really new to all this so I'm not even sure if this is a valid approach.</p> <p>Based on the first answer, I reworked my example. Still not working? Am I getting warm?</p> <pre><code>namespace WpfApplication1 { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { public MainWindow() { var someClass = new SomeClass(); someClass.Test(); InitializeComponent(); } } public class SomeClass { [LogCall] public void Test() { } } public class LogCallInterceptor : IMethodBeforeAdvice { public void Before(MethodInfo method, object[] args, object target) { Debug.Write(method.Name); } } [Serializable] [AttributeUsage(AttributeTargets.Method)] public class LogCallAttribute : Attribute { } } </code></pre> <p>And the new app.config</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;configuration&gt; &lt;spring&gt; &lt;objects xmlns="http://www.springframework.net"&gt; &lt;object id="TestLogAdvice" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop"&gt; &lt;property name="advice"&gt; &lt;object type="WpfApplication1.LogCallInterceptor, WpfApplication1" /&gt; &lt;/property&gt; &lt;property name="attribute" value="WpfApplication1.LogCallAttribute, WpfApplication1" /&gt; &lt;/object&gt; &lt;/objects&gt; &lt;object id="mySomeClass" type="Spring.Aop.Framework.ProxyFactoryObject"&gt; &lt;property name="target"&gt; &lt;object id="mySomeClassTarget" type="WpfApplication1.SomeClass"/&gt; &lt;/property&gt; &lt;property name="interceptorNames"&gt; &lt;list&gt; &lt;value&gt;TestLogAdvice&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/object&gt; &lt;/spring&gt; &lt;runtime&gt; &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt; &lt;dependentAssembly&gt; &lt;assemblyIdentity name="Spring.Core" publicKeyToken="65e474d141e25e07" culture="neutral" /&gt; &lt;bindingRedirect oldVersion="0.0.0.0-1.3.2.40943" newVersion="1.3.2.40943" /&gt; &lt;/dependentAssembly&gt; &lt;dependentAssembly&gt; &lt;assemblyIdentity name="Spring.Aop" publicKeyToken="65e474d141e25e07" culture="neutral" /&gt; &lt;bindingRedirect oldVersion="0.0.0.0-1.3.2.40943" newVersion="1.3.2.40943" /&gt; &lt;/dependentAssembly&gt; &lt;/assemblyBinding&gt; &lt;/runtime&gt; &lt;/configuration&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload