日志AOP的实现

 1     /// <summary>
 2     /// 日志AOP拦截
 3     /// </summary>
 4     [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
 5     public class LogInterceptorAttribute : ContextAttribute, IContributeObjectSink
 6     {
 7         public LogInterceptorAttribute()
 8             : base("LogInterceptor")
 9         { }
10 
11         /// <summary>
12         /// 实现IContributeObjectSink接口当中的消息接收器接口
13         /// </summary>
14         /// <param name="obj"></param>
15         /// <param name="next"></param>
16         /// <returns></returns>
17         public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink next)
18         {
19             return new LogInterceptorHandler(next);
20         }
21     }
22     /// <summary>
23     /// 日志拦截操作
24     /// </summary>
25     public sealed class LogInterceptorHandler : IMessageSink
26     {
27         //下一个接收器
28         private IMessageSink nextSink;
29         public IMessageSink NextSink
30         {
31             get { return nextSink; }
32         }
33         public LogInterceptorHandler(IMessageSink nextSink)
34         {
35             this.nextSink = nextSink;
36         }
37 
38         public IMessage SyncProcessMessage(IMessage msg)
39         {
40             OnActionExecuted(msg);
41             var retMsg = nextSink.SyncProcessMessage(msg);
42             OnActionExecuting(retMsg);
43             return retMsg;
44         }
45         public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
46         {
47             return null;
48         }
49 
50         /// <summary>
51         /// 方法执行前
52         /// </summary>
53         /// <param name="msg"></param>
54         private void OnActionExecuted(IMessage msg)
55         {
56 
57         }
58 
59         /// <summary>
60         /// 方法执行后
61         /// </summary>
62         /// <param name="msg"></param>
63         private void OnActionExecuting(IMessage msg)
64         {
65 
66         }
67     }
原文地址:https://www.cnblogs.com/wms01/p/8252985.html