接下来我们看一下SpringNetAop层的内容;此层主要是实现一些AOP的代码,以及特性和一个统一调用spring.net的类;记录日志我们使用Log4Net来实现;
1:Aspects 文件夹里存放的几个类分别是Spring.net几种AOP的通知模式;因为本实例主要是要演示异常的调用所以我们调用的是类LoggingThrowsAdvice.cs;有关Log4Net的运用大家可以搜索一下,不是本文的重点;这边有个要注意此处是类库运用Log4net,所以在读取配置文件时要写成@"bin\\Log4Net.config";而配置文件Log4Net.config属性要设置为:始终复制 嵌入的资源;
1:Aspects 文件夹里存放的几个类分别是Spring.net几种AOP的通知模式;因为本实例主要是要演示异常的调用所以我们调用的是类LoggingThrowsAdvice.cs;有关Log4Net的运用大家可以搜索一下,不是本文的重点;这边有个要注意此处是类库运用Log4net,所以在读取配置文件时要写成@"bin\\Log4Net.config";而配置文件Log4Net.config属性要设置为:始终复制 嵌入的资源;
using System.Linq;
using System.Text;
using SpringNetCommon;
using System.Reflection;
using Spring.Aop;
using log4net; using log4net.Core; [assembly: log4net.Config.XmlConfigurator(ConfigFile = @"bin\\Log4Net.config", Watch = true)] namespace SpringNetAop.Aspects { public class LoggingThrowsAdvice:IThrowsAdvice { private ILog logger; public LoggingThrowsAdvice() { logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("Log4Net.config")); } public void AfterThrowing(Exception ex) { logger.Warn( String.Format("异常的内容为: {0}", ex)); } } }
Log4Net.config配置的内容如下:配置实现的功能是每天生成一个记录文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections> <section name="log4net" type="System.Configuration.IgnoreSectionHandler" /> </configSections> <log4net> <appender name="RollingLogRootFileAppender" type="log4net.Appender.RollingFileAppender"> <!--日志的路径--> <file value=".\Log\WanLog" /> <!--是否覆盖,默认是追加true--> <appendToFile value="true"/> <!--文件滚动周期(每日创建新日志文件)--> <datePattern value="yyyyMMdd".txt""/> <!--设置无限备份=-1 ,最大备份数为1000--> <maxSizeRollBackups value="1000"/> <!--名称是否可以更改为false为可以更改--> <staticLogFileName value="false" /> <!--文件滚动选项Composite表示根据日期和大小来滚动--> <rollingStyle value="Composite" /> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss}[%t][%-5p][%c]%m%n%exception%n" /> </layout> </appender> <root> <level value="All" /> <appender-ref ref="RollingLogRootFileAppender" /> </root> </log4net> </configuration>
2:Attributes存放的是一个重写的特性;spring.net特性来定位要调用AOP;这边主要注意是要继承Attribute;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SpringNetAop.Attributes
{
public class LoggingAttributes:Attribute
{
public LoggingAttributes()
{
}
}
}
3:SpringNetHelp文件夹存放的是一个统一调用spring.net的类;因为我把spring.net的配置文件存放在UI层;所以要注意调用的路径问题;配置文件我们在UI层进行;
using Spring.Core;
using Spring.Context;
using Spring.Context.Support;
using Spring.Core.IO;
using Spring.Objects.Factory;
using Spring.Objects.Factory.Xml; namespace SpringNetAop.SpringNetHelp { public class GetAppContext { private static IApplicationContext applicationContext = null; public GetAppContext() { } public static IApplicationContext ApplicationContext { get { applicationContext = new XmlApplicationContext("~/Config/Objects.xml"); return applicationContext; } } } }
接下来我们看一下逻辑层跟数据层的代码;
首先看一下数据层的代码;我们为要进行异常记录的方法加上特性[LoggingAttributes]
using System.Linq;
using System.Text;
using ISpringNetDAL;
using SpringNetAop.Attributes;
namespace SpringNetDAL
{
public class UserOperateDAL:IUserOperateDAL { [LoggingAttributes] public string UserLogin(string UserName) { throw new Exception("我是DAL层的异常"); } } }
看BLL层的具体代码如下:
using Spring.Core;
using SpringNetAop;
using Spring.Core.IO;
using Spring.Objects.Factory.Xml;
using Spring.Objects.Factory;
using Spring.Context.Support; using Spring.Context; namespace SpringNetBLL { public class UserOperate:BaseBLL { private IUserOperateDAL _userOperatedal = null; public UserOperate() { } [LoggingAttributes] public override string UserLogin(string UserName) { // throw new Exception("我是BLL层异常内容啊"); _userOperatedal = (IUserOperateDAL)SpringNetAop.SpringNetHelp.GetAppContext.ApplicationContext.GetObject("UserOperateDal"); return _userOperatedal.UserLogin(UserName); } } }
<?xml version="1.0"?> <configuration> <configSections> <sectionGroup name="spring"> <!--提供Spring对应用程序上下文的支持--> <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/> <!--提供Spring对 对象容器的支持--> <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/> </sectionGroup> </configSections> <spring> <context> <resource uri="file://~/Config/Objects.xml"/> <!--<resource uri="assembly://SpringNetUI/SpringNetUI.Config/Objects.xml"/>--> </context> </spring> </configuration>
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <object name="UserOperateDal" type="SpringNetDAL.UserOperateDAL,SpringNetDAL"/> <!--环绕通知拦截 运用特性--> <object id="aroundAdvisor" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop"> <property name="Advice"> <object type="SpringNetAop.Aspects.LoggingAroundAdvice, SpringNetAop" /> </property> <property name="Attribute" value="SpringNetAop.Attributes.LoggingAttributes, SpringNetAop" /> </object> <!--后置通知拦截 运用名称--> <object id="afterAdvisor" type="Spring.Aop.Support.NameMatchMethodPointcutAdvisor, Spring.Aop"> <property name="Advice"> <object type="SpringNetAop.Aspects.LoggingAfterAdvice, SpringNetAop" /> </property> <property name="MappedNames"> <list> <value>*UserLogin</value> </list> </property> </object> <!--异常通知拦截 程序所有都支持 不设置条件--> <object id="throwsAdvice" type="SpringNetAop.Aspects.LoggingThrowsAdvice, SpringNetAop" /> <object id="myServiceCommand" type="Spring.Aop.Framework.ProxyFactoryObject"> <property name="Target"> <object type="SpringNetBLL.UserOperate, SpringNetBLL" /> </property> <property name="InterceptorNames"> <list> <value>aroundAdvisor</value> <value>afterAdvisor</value> <value>throwsAdvice</value> </list> </property> </object> </objects>