Let's take a look at the contents of the SPRINGNETAOP layer, this layer is mainly to implement some AOP code, as well as the characteristics and a unified call Spring.net class; logging we use log4net to achieve;
1:aspects The several classes stored in the folder are the notification modes of spring.net several AOP, because this example is mainly to demonstrate the invocation of the exception, so we call the class LoggingThrowsAdvice.cs; the use of log4net can be searched, not the focus of this article; here's a note. The class library uses log4net, so write @ "Bin\\log4net.config" When reading the configuration file, while the profile Log4net.config property is set to: Always copy embedded resources;
1:aspects The several classes stored in the folder are the notification modes of spring.net several AOP, because this example is mainly to demonstrate the invocation of the exception, so we call the class LoggingThrowsAdvice.cs; the use of log4net can be searched, not the focus of this article; here's a note. The class library uses log4net, so write @ "Bin\\log4net.config" When reading the configuration file, while the profile Log4net.config property is set to: Always copy embedded resources;
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 (contents of String.Format (
The contents of the Log4net.config configuration are as follows: The function of configuration implementation is to generate a log file every day
<?XML version= "1.0" encoding= "Utf-8"?><Configuration><Configsections><SectionName="Log4net"Type="System.Configuration.IgnoreSectionHandler"/></Configsections><Log4net><AppenderName="Rollinglogrootfileappender"Type="Log4net. Appender.rollingfileappender "><!--Log Path--<FileValue=". \log\wanlog"/><!--Overwrite, default is append true--><AppendtofileValue="True"/><!--File rollover cycle (create new log files daily)--<DatepatternValue="Yyyymmdd" txt" " /><!--Set unlimited backup =-1 with a maximum backup count of 1000--><MaxsizerollbackupsValue="/>"<!--Whether the name can be changed to false to change--<StaticlogfilenameValue="False"/><!--The file scrolling option, composite, means scrolling by date and size.<RollingstyleValue="Composite"/><LayoutType="Log4net. Layout.patternlayout "><ParamName="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 "/> </< Span class= "title" >root> </log4net> </CONFIGURATION>
The 2:attributes store is an overriding feature; The Spring.net feature is positioned to invoke AOP; The main note here is to inherit the attribute;
Using system.collections.generic;using system.linq;using system.text;namespace springnetaop.attributes{ classLoggingattributes:attribute {public loggingattributes () { } }}
3:springnethelp folder is a unified call Spring.net class, because I put the spring.net configuration file in the UI layer, so pay attention to the path of the call, configuration file We do in the UI layer;
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 {Applicationco ntext = new Xmlapplicationcontext ("~/config/objects.xml"); return applicationcontext;} } }}
Next we look at the logic layer and the data layer code;
Let's look at the data layer code first, and we'll add properties to the method that we want to record the exception [Loggingattributes]
Using System.Linq; using System.Text; using Ispringnetdal; using Springnetaop.attributes; namespace springnetdal{ string userlogin (new Exception (" I am an exception to the DAL layer"); }}}
See the specific code for the BLL layer as follows:
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 ("I am the abnormal content of the BLL layer"); _useroperatedal = (Iuseroperatedal) SpringNetAop.SpringNetHelp.GetAppContext.ApplicationContext.GetObject ("useroperatedal"); return _useroperatedal.userlogin (UserName);} }}
<?XML version= "1.0"?><Configuration><Configsections><SectiongroupName="Spring" ><!--Provides spring support for application contexts--<SectionName="Context"Type="Spring.Context.Support.ContextHandler, Spring.core"/><!--Provides spring support for object containers--<SectionName="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"?><Objectsxmlns="Http://www.springframework.net"Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance" ><ObjectName="Useroperatedal"Type="Springnetdal.useroperatedal,springnetdal"/><!--Surround notification interception using features--<ObjectId="Aroundadvisor"Type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, SPRING.AOP" ><PropertyName="Advice" ><ObjectType="SpringNetAop.Aspects.LoggingAroundAdvice, Springnetaop"/></Property><PropertyName="Attribute"Value="SpringNetAop.Attributes.LoggingAttributes, Springnetaop"/></Object><!--Post-notification interception application name--<ObjectId="Afteradvisor"Type="Spring.Aop.Support.NameMatchMethodPointcutAdvisor, SPRING.AOP" ><PropertyName="Advice" ><ObjectType="SpringNetAop.Aspects.LoggingAfterAdvice, Springnetaop"/></Property><PropertyName="Mappednames" ><List><Value>*userlogin</Value></List></Property></Object><!--Exception notification blocker all support not set condition--<ObjectId="Throwsadvice"Type="SpringNetAop.Aspects.LoggingThrowsAdvice, Springnetaop"/><ObjectId="Myservicecommand"Type="Spring.Aop.Framework.ProxyFactoryObject" ><PropertyName="Target" ><ObjectType="Springnetbll.useroperate, Springnetbll"/></Property><PropertyName="Interceptornames" ><list> <value>aroundadvisor </value> < value>afteradvisor</value > <value>throwsadvice</< span class= "tag" >value> </list > </property> </ object></ Objects>
Spring.net Exception Handling