日志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     }
复制代码

 

posted on   Jade_K  阅读(352)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
历史上的今天:
2017-01-09 mvc和webapi同一解决方案调试办法

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示