If a key is required for each methodCodeWill be messy.
Later, I studied it. It's quite simple to use Castle. Just read other people's demos and copy the code.
Because the Framework is a class library, the configuration file of aspectsharp can only be embedded with resources.
To be lazy, I saved it directly using a TXT file.
Steps:
1. Create a configuration file to save the code configuration to be injected
Import wildfish. dataaccess. Base. AOP. interceptors in wildfish
Interceptors ["logger": loggerinterceptor]
Aspect ibasedataaccesslogger for [assignablefrom (ibasedataaccess)]
Pointcut method (*)
Advice ("logger ")
End
End
The above indicates that my interceptor exists in the wildfish. dataaccess. Base. AOP. interceptors space of wildfish. dll.
Declares an interceptors [Key = logger, object = loggerinterceptor]
Defines an aspect object for implementing the ibasedataaccess interface.
Pointcut method (*) indicates that I want to capture all the methods
Advice ("logger") indicates that I want to inject the logger invoke method into these methods.
2. Define a loggerinterceptor
Using System;
Using System. Collections. Generic;
Using System. text;
Using Aopalliance. Intercept;
Using System. reflection;
Using Wildfish. systemframework. base;
Namespace Wildfish. dataaccess. Base. AOP. interceptors
{
/**/ /// <Summary>
///Logger interceptor
/// </Summary>
Public Class Loggerinterceptor: imethodinterceptor
{
/**/ /// <Summary>
///Constructor
/// </Summary>
Public Loggerinterceptor ()
{
}
/**/ /// <Summary>
/// Invoke Method
/// </Summary>
/// <Param name = "invocation"> Method Invocation </Param>
/// <Returns> Result </Returns>
Public Object Invoke (imethodinvocation Invocation)
{
Logmethod (Invocation. method );
// Logarguments (Invocation. Arguments );
Object Result = Invocation. Proceed ();
// Logreturn (result );
Return Result;
}
/**/ /// <Summary>
///Log Method
/// </Summary>
/// <Param name = "method">Methodbase</Param>
Private Void Logmethod (methodbase method)
{
Applicationlog. writedebug (String. Format ("[Aspect # logger interceptor] [Method] Name: {0}", Method. Name ));
}
/**/ /// <Summary>
///Log Return Value
/// </Summary>
/// <Param name = "result">Result</Param>
Private Void Logreturn ( Object Result)
{
Applicationlog. writedebug (String. Format ("[Aspect # logger interceptor] [Return] value: {0}", Result ));
}
/**/ /// <Summary>
///Log arguments
/// </Summary>
/// <Param name = "arguments">Arguments</Param>
Private Void Logarguments ( Object [] Arguments)
{
For ( Int I = 0 ; I < Arguments. length; I ++ )
{
Applicationlog. writedebug (String. Format ("[Aspect # logger interceptor] [argument] index: {0}. Value: {1}", I, arguments [I]);
}
}
}
}
Implement an interface imethodinterceptor
The main method is the invoke method.
Indicates that
1. Record method name
2. Call Method
3. Return call Return Value
3. Use aspectengine to wrap the object you want to obtain Ibasedataaccess cloneddataaccess = Basedataaccess. Clone () As Ibasedataaccess;
If (Dataaccesssection. needtrace)
{
Cloneddataaccess=_ Engine. wrapinterface (Typeof(Ibasedataaccess), cloneddataaccess)AsIbasedataaccess;
}
Return Cloneddataaccess;
4. The switch is defined in wildfish. config. xml.
If this parameter is set to true, method injection is automatically called. Otherwise, method injection is not called.
After running, the following log is obtained:
00:26:34, 995 [524] Debug wildfishlogger-[aspect # logger interceptor] [Method] Name: Clone
00:26:46, 552 [524] Debug wildfishlogger-[aspect # logger interceptor] [Method] Name: Clone
00:26:46, 803 [524] Debug wildfishlogger-[aspect # logger interceptor] [Method] Name: Clone
00:29:49, 670 [632] Debug wildfishlogger-[aspect # logger interceptor] [Method] Name: filldatasetbyall
00:29:59, 295 [632] Debug wildfishlogger-[aspect # logger interceptor] [Method] Name: checkexist
00:29:59, 395 [632] Debug wildfishlogger-[aspect # logger interceptor] [Method] Name: insertobjectdata
00:29:59, 495 [632] Debug wildfishlogger-[aspect # logger interceptor] [Method] Name: filldatasetbyall
:)