各位朋友们,小弟在使用spring.net 这个框架来进行AOP编程,记录系统错误日志,但是那个错误通知老是触发不了。配置xml如下:
<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd"> <object id="Test" type="LogAopSlution.Test, LogAopSlution" /> <object id="ExceptionThrowAdvice" type="LogAopSlution.ExceptionThrowAdvice, LogAopSlution" /> <object id="WriteLogProxy" type="Spring.Aop.Framework.ProxyFactoryObject, Spring.Aop" > <!--<property name="ProxyInterfaces"> <list> <value>LogAopSlution.ITest, LogAopSlution</value> </list> </property>--> <!--<property name="ProxyTargetType" value="true"/>--> <property name="Target"> <ref object="Test" /> </property> <property name="InterceptorNames"> <list> <value>ExceptionThrowAdvice</value> </list> </property> </object> </objects>
ExceptionThrowAdvice异常通知类
实现如下:
using System; using System.Reflection; using Spring.Aop; namespace LogAopSlution { /// <summary> /// 异常信息的环绕通知 /// </summary> public class ExceptionThrowAdvice : IThrowsAdvice { #region Members private readonly TrackProvider _logInstance; #endregion #region Constructor /// <summary> /// 初始化日志记录处理器的实例 /// </summary> public ExceptionThrowAdvice() { _logInstance = TrackProvider.GetInstance(); } #endregion #region Methods /// <summary> /// 根据异常抛出时机记录异常日志 /// </summary> /// <param name="method">方法信息表</param> /// <param name="args">参数</param> /// <param name="target">目标对象</param> /// <param name="exception">异常对象</param> public void AfterThrowing(MethodInfo method, Object[] args, Object target, Exception exception) { _logInstance.WriteLog(exception); } #endregion } }
Test类的一个简单的测试功能:
namespace LogAopSlution { public class Test { public int Caculate() { try { int x = 9; int y = 0; int z = x / y; return z; } catch (System.Exception ex) { //log4net.ILog Log = log4net.LogManager.GetLogger(typeof (LogAopSlution.Test).FullName); //Log.Debug(ex); } return 0; } } }
Program的调用如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Spring.Context; using Spring.Context.Support; namespace LogAopSlution { class Program { private static IApplicationContext _ctx; static void Main(string[] args) { Test test = ctx["WriteLogProxy"] as Test; Console.Write(test.Caculate()); Console.ReadKey(); } private static IApplicationContext ctx { get { return _ctx ?? (_ctx = ContextRegistry.GetContext()); } } } }
App.config:
<?xml version="1.0"?> <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> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> </configSections> <!--spring配置--> <spring> <context> <resource uri="~/object.xml"/> </context> </spring> <!-- Log输出定义 --> <log4net> <!-- Define some output appenders --> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="test.txt"/> <appendToFile value="true"/> <maxSizeRollBackups value="10"/> <maximumFileSize value="1024KB"/> <rollingStyle value="Size"/> <staticLogFileName value="true"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/> </layout> </appender> <!--<logger name="Spring"> <level value="INFO"/> <appender-ref ref="LogFileAppender"/> </logger> <logger name="NHibernate"> <level value="INFO"/> <appender-ref ref="LogFileAppender"/> </logger> <logger name="AppLog"> <level value="ALL"/> <appender-ref ref="AppLogAppender"/> </logger>--> <root> <level value="DEBUG" /> <appender-ref ref="RollingLogFileAppender" /> </root> </log4net> <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
调用的时候好使触发不了通知,但是注入的Test类确实可以拿到实例的,请问我注册环绕通知的xml配置有什么题吗?
恳请各位朋友指点,谢谢!