为Wildfish框架增加方法调用日志-[Aspectsharp]
如果每个方法都需要增加一个Key的话,那么代码会很杂乱。
后来研究了一下,用Castle挺简单的,看别人的Demo,代码Copy一下即可。
由于框架是一个类库,所以AspectSharp的配置文件只能是用嵌入资源。
为了偷懒,我就直接用txt文件保存
需要做的步骤:
1。建立一个配置文件保存需要注入的代码配置
import Wildfish.DataAccess.Base.AOP.Interceptors in Wildfish
interceptors [ "logger" : LoggerInterceptor ]
aspect IBaseDataAccessLogger for [ assignableFrom(IBaseDataAccess) ]
pointcut method(*)
advice("logger")
end
end
以上表示我的Interceptor存在Wildfish.dll的Wildfish.DataAccess.Base.AOP.Interceptors空间下
声明了一个interceptors [key=logger,object=LoggerInterceptor ]
定义了一个Aspect 适用于实现了IBaseDataAccess接口的对象。
pointcut method(*)表示我要捕获所有的方法
advice("logger") 表示我要往这些方法注入logger的invoke方法
2,定义一个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]));
}
}
}
}
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]));
}
}
}
}
实现一个接口IMethodInterceptor
主要是Invoke方法
里面的调用表明
1。记录方法名字
2。调用方法
3。返回调用返回值
3。在需要获取该对象的地方,通过AspectEngine 包装一下你要获取的那个对象
IBaseDataAccess clonedDataAccess=baseDataAccess.Clone() as IBaseDataAccess;
if (dataAccessSection.NeedTrace)
{
clonedDataAccess = _engine.WrapInterface(typeof(IBaseDataAccess), clonedDataAccess) as IBaseDataAccess;
}
return clonedDataAccess;
if (dataAccessSection.NeedTrace)
{
clonedDataAccess = _engine.WrapInterface(typeof(IBaseDataAccess), clonedDataAccess) as IBaseDataAccess;
}
return clonedDataAccess;
4.开关定义在wildfish.config.xml里面
如果为true,则自动调用方法注入,否则,不调用。
run 完之后,得到如下log
2006-04-05 00:26:34,995 [524] DEBUG WildfishLogger - [Aspect# Logger Interceptor][method]Name: Clone
2006-04-05 00:26:46,552 [524] DEBUG WildfishLogger - [Aspect# Logger Interceptor][method]Name: Clone
2006-04-05 00:26:46,803 [524] DEBUG WildfishLogger - [Aspect# Logger Interceptor][method]Name: Clone
2006-04-05 00:29:49,670 [632] DEBUG WildfishLogger - [Aspect# Logger Interceptor][method]Name: FillDataSetByAll
2006-04-05 00:29:59,295 [632] DEBUG WildfishLogger - [Aspect# Logger Interceptor][method]Name: CheckExist
2006-04-05 00:29:59,395 [632] DEBUG WildfishLogger - [Aspect# Logger Interceptor][method]Name: InsertObjectData
2006-04-05 00:29:59,495 [632] DEBUG WildfishLogger - [Aspect# Logger Interceptor][method]Name: FillDataSetByAll
:)