使用aop的Spring.net日志记录示例 [英] Spring.net Logging Example using aop

查看:68
本文介绍了使用aop的Spring.net日志记录示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Spring.Net,并且正在尝试一些简单的方法,但是没有用.我想记录任何用LogCall

I'm learning Spring.Net and am trying something simple, which is not working. I want to log any method calls decorated with LogCall

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

And here's the 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>

我真的很陌生,所以我什至不确定这是否有效.

I'm really new to all this so I'm not even sure if this is a valid approach.

根据第一个答案,我重新制作了示例.还是行不通?我变暖了吗?

Based on the first answer, I reworked my example. Still not working? Am I getting warm?

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
    {
    }
}

还有新的app.config

And the new 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>
    <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>

推荐答案

您正在使用spring aop来设置日志记录,这是一种有效的方法.您需要考虑几件事:

You are using spring aop to set up logging, and this is a valid approach. There are a couple of things you have to consider:

Spring AOP使用动态代理来装饰带有(记录)建议的类.该代理拦截对您的对象的调用并应用日志记录建议.在您的类中,您可以从类本身中的 in 中调用Test方法.这样,动态代理永远不会拦截呼叫,并且不会进行任何日志记录.

Spring AOP uses a dynamic proxy to decorate a class with (logging) advices. This proxy intercepts calls to your object and applies the logging advice. In your class, you call the Test method from within the class itself. This way the dynamic proxy can never intercept the call and no logging will take place.

从您的配置中,我读到您定义了必须运行的建议(您的LogCallInterceptor)和位置(与您的属性匹配的方法),但是我看不到定义代理工厂的位置. Spring必须创建一个代理,并且您必须告诉它在哪里做.

From your config I read that you define which advice has to run (your LogCallInterceptor) and where (methods matching your attribute), but I don't see where you define your proxy factory. Spring has to create a proxy and you have to tell it where to do it.

aop快速入门是了解如何执行此操作的好地方.实际上,第一个示例是一个日志记录示例,它非常适用于您的问题.我猜想在阅读了快速入门的第一部分(第38.2.1章)之后,您将知道该怎么做.

The aop quickstart is a good place to find out how to do this. In fact, one of the first examples is a logging example, which is very applicable to your question. I'm guessing that after reading the first part of the quickstart (chapter 38.2.1.) you'll know what to do to get this working.

Spring AOP是一种强大的技术,但是起初可能很难掌握.您已经很顺利了.

Spring AOP is a powerful technique, but can be a bit hard to master at first. You're well on your way already.

编辑1

我看到您已更新您的问题.我想你快到了.

I see you've updated your question. You're almost there, I think.

现在,您将直接通过代码创建一个SomeClass实例.这样,Spring再次没有机会创建它的代理.您必须委派SomeClass的创建 到弹簧容器:

Now you're creating a SomeClass instance directly from code. This way, Spring again doesn't get a chance to create it's proxy. You have to delegate the creation of SomeClass to the spring container:

public MainWindow()
{
  // normally speaking, we should not create the container here,
  // but that's another subject
  var ctx = ContexRegistry.GetContext(); // init spring container
  var someClass = (SomeClass) ctx["mySomeClass"];
  someClass.Test();
  InitializeComponent();
}

这样,someClass将保留代理而不是目标.

This way, someClass will hold the proxy instead of the target.

此后,仍然存在一个问题(提示).

After this, there's one problem remaining (hint).

编辑2

您的Test方法必须是虚拟的,否则spring不能创建基于继承的代理. (或者您的课程必须实现一个或多个接口).

Your're Test method has to be virtual, otherwise spring can't create an inheritance based proxy. (or your class has to implement one or more interfaces).

使用自动代理进行配置

以下app.config使用DefaultAdvisorAutoProxyCreator.这将确保您不必为要向其应用日志记录顾问的每个类创建代理工厂. DefaultAdvisorAutoProxyCreator将找到所有带有LogCallAttribute的对象,并为其创建代理.

The following app.config uses an DefaultAdvisorAutoProxyCreator. This will make sure you don't have to create a proxy factory for each and every class you want to apply your logging advisor to. The DefaultAdvisorAutoProxyCreator will find all objects with LogCallAttributes and create a proxy for them.

<?xml version="1.0" encoding="utf-8"?>

<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.springframework.net">

      <object id="TestLogAdvice" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop">
        <property name="advice">
          <object type="q8029460.LogCallInterceptor, q8029460" />
        </property>
        <property name="attribute" value="q8029460.LogCallAttribute, q8029460" />
      </object>

      <object id="ProxyCreator" type="Spring.Aop.Framework.AutoProxy.DefaultAdvisorAutoProxyCreator, Spring.Aop"/>

      <object id="mySomeClass" type="q8029460.MyClass, q8029460" />

    </objects>
  </spring>

</configuration>

这篇关于使用aop的Spring.net日志记录示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆