我在学习Spring.Net,并尝试一些简单的操作,但这种操作不起作用。我想记录的任何方法调用饰LogCall
使用aop的Spring.net日志记录示例
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
{
}
}
而这里的App.config中
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<spring>
<objects xmlns="http://www.springframework.net">
<object id="TestLogAdvice" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop">
<property name="advice">
<object type="WpfApplication1.LogCallInterceptor, WpfApplication1" />
</property>
<property name="attribute" value="WpfApplication1.LogCallAttribute, WpfApplication1" />
</object>
</objects>
</spring>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Spring.Core" publicKeyToken="65e474d141e25e07" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.3.2.40943" newVersion="1.3.2.40943" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Spring.Aop" publicKeyToken="65e474d141e25e07" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.3.2.40943" newVersion="1.3.2.40943" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
我真的很新的这一切,所以我甚至不知道这是否是一个有效的做法。
根据第一个答案,我重新修改了我的例子。还是行不通?我变暖了吗?
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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
{
}
}
而且
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<spring>
<objects xmlns="http://www.springframework.net">
<object id="TestLogAdvice" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop">
<property name="advice">
<object type="WpfApplication1.LogCallInterceptor, WpfApplication1" />
</property>
<property name="attribute" value="WpfApplication1.LogCallAttribute, WpfApplication1" />
</object>
</objects>
<object id="mySomeClass" type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="target">
<object id="mySomeClassTarget" type="WpfApplication1.SomeClass"/>
</property>
<property name="interceptorNames">
<list>
<value>TestLogAdvice</value>
</list>
</property>
</object>
</spring>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Spring.Core" publicKeyToken="65e474d141e25e07" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.3.2.40943" newVersion="1.3.2.40943" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Spring.Aop" publicKeyToken="65e474d141e25e07" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.3.2.40943" newVersion="1.3.2.40943" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
我已经更新了我的答案,你越来越近了。 – Marijn
您的xml中存在错误:应该移动。 – Marijn
如果这是你的完整app.config,它也不会工作,我会在我的答案下面写一个注释。 – Marijn