日志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 }
作者:王家大人
出处:http://wms01.cnblogs.com
限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。
声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。