本文介绍了PostSharp IL - 织造 - 思考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我使用Postsharp框架来简化应用程序的方法记录的负担考虑。 基本上,它可以让我来装饰日志记录属性的方法,并在编译时需要注射到IL日志代码。我喜欢这个解决方案,因为它使噪音出来的不屑时间码的环境。 的任何想法,经验和更好的方法?I am considering using Postsharp framework to ease the burden of application method logging.It basically allows me to adorn methods with logging attribute and at compile time injects the logging code needed into the il. I like this solution as it keeps the noise out of the deign time code environment.Any thoughts, experiences or better alternatives?推荐答案我申请与使用AOP温莎城堡DynamicProxies记录。我已经使用城堡为它的IoC容器,所以使用它的AOP是阻力最小的路径我。如果您想了解更多的信息让我知道,我在整理代码为释放它作为一个博客帖子I apply logging with AOP using Castle Windsor DynamicProxies. I was already using Castle for it's IoC container, so using it for AOP was the path of least resistence for me. If you want more info let me know, I'm in the process of tidying the code up for releasing it as a blog post修改好吧,这里的基本截击代码,faily基本的,但它确实我需要的一切。有两个intercepters,人登录everyhing和其他允许您定义方法名,以便更细粒度的记录。该解决方案是faily依赖于温莎城堡Ok, here's the basic Intercepter code, faily basic but it does everything I need. There are two intercepters, one logs everyhing and the other allows you to define method names to allow for more fine grained logging. This solution is faily dependant on Castle Windsor 抽象基类namespace Tools.CastleWindsor.Interceptors{using System;using System.Text;using Castle.Core.Interceptor;using Castle.Core.Logging;public abstract class AbstractLoggingInterceptor : IInterceptor{ protected readonly ILoggerFactory logFactory; protected AbstractLoggingInterceptor(ILoggerFactory logFactory) { this.logFactory = logFactory; } public virtual void Intercept(IInvocation invocation) { ILogger logger = logFactory.Create(invocation.TargetType); try { StringBuilder sb = null; if (logger.IsDebugEnabled) { sb = new StringBuilder(invocation.TargetType.FullName).AppendFormat(".{0}(", invocation.Method); for (int i = 0; i < invocation.Arguments.Length; i++) { if (i > 0) sb.Append(", "); sb.Append(invocation.Arguments[i]); } sb.Append(")"); logger.Debug(sb.ToString()); } invocation.Proceed(); if (logger.IsDebugEnabled && invocation.ReturnValue != null) { logger.Debug("Result of " + sb + " is: " + invocation.ReturnValue); } } catch (Exception e) { logger.Error(string.Empty, e); throw; } }}} 全部记录Implemnetationnamespace Tools.CastleWindsor.Interceptors{using Castle.Core.Logging;public class LoggingInterceptor : AbstractLoggingInterceptor{ public LoggingInterceptor(ILoggerFactory logFactory) : base(logFactory) { }}} 方式登录namespace Tools.CastleWindsor.Interceptors{using Castle.Core.Interceptor;using Castle.Core.Logging;using System.Linq;public class MethodLoggingInterceptor : AbstractLoggingInterceptor{ private readonly string[] methodNames; public MethodLoggingInterceptor(string[] methodNames, ILoggerFactory logFactory) : base(logFactory) { this.methodNames = methodNames; } public override void Intercept(IInvocation invocation) { if ( methodNames.Contains(invocation.Method.Name) ) base.Intercept(invocation); }}} 这篇关于PostSharp IL - 织造 - 思考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!