Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 后夏普-伊尔编织-思想_C#_Visual Studio_Logging_Aop_Postsharp - Fatal编程技术网

C# 后夏普-伊尔编织-思想

C# 后夏普-伊尔编织-思想,c#,visual-studio,logging,aop,postsharp,C#,Visual Studio,Logging,Aop,Postsharp,我正在考虑使用Postsharp框架来减轻应用程序方法日志记录的负担。 它基本上允许我用logging属性修饰方法,并在编译时将所需的日志代码注入il。我喜欢这个解决方案,因为它将噪音排除在设计时间代码环境之外。 有什么想法、经验或更好的选择吗?我使用Castle Windsor DynamicProxies使用AOP进行日志记录。我已经在使用Castle作为它的IoC容器,所以将它用于AOP对我来说是阻力最小的途径。如果你想了解更多信息,请告诉我,我正在整理代码,以便将其作为博客发布 编辑 好

我正在考虑使用Postsharp框架来减轻应用程序方法日志记录的负担。 它基本上允许我用logging属性修饰方法,并在编译时将所需的日志代码注入il。我喜欢这个解决方案,因为它将噪音排除在设计时间代码环境之外。
有什么想法、经验或更好的选择吗?

我使用Castle Windsor DynamicProxies使用AOP进行日志记录。我已经在使用Castle作为它的IoC容器,所以将它用于AOP对我来说是阻力最小的途径。如果你想了解更多信息,请告诉我,我正在整理代码,以便将其作为博客发布

编辑

好的,这是基本的截取代码,faily basic,但它做了我需要的一切。有两个拦截器,一个记录每一行,另一个允许您定义方法名称以允许更细粒度的日志记录。该解决方案完全依赖于温莎城堡

抽象基类

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;
        }
    }
}
}
方法日志记录

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);
    }
}
}

+邮政总局1号。已经使用了很多东西(包括一些在C代码中添加前置条件和后置条件的尝试),不知道如果没有它我将如何实现…

这在一定程度上取决于您将开发和支持项目多长时间。当然,IL编织是一种很好的技术,但是如果IL和/或程序集元数据格式再次更改(如1.1和2.0之间的更改),并且这些更改使工具与新格式不兼容,会发生什么呢

如果您依赖该工具,则在该工具支持之前,它会阻止您升级技术。如果没有关于这方面的保证(或者即使开发将继续进行,尽管看起来确实有可能),那么我将非常谨慎地在长期项目中使用它


短期来说,没问题。

我也这么想。我很想听听人们的想法。是的,我被窃听了几个星期——但我认为编译时的开销是值得的,因为没有日志代码,很高兴听到有人使用过它。您好,如果您能提供一个快速演示,那就太好了-它是否可以通过方法处理属性我不喜欢使用属性来处理横切的问题,如日志记录、验证等。我使用基于Boo的外部DSL来配置IoC容器,并在那里分配日志记录。这意味着我可以添加/删除日志,而无需为快速响应干杯。感觉我将进入一个xml配置和IOC的世界,只是为了记录。不,说不就行了!Castle拥有流畅的界面和DSL。我是DSL的超级粉丝,它被称为Binsor,你可以在这里找到更多信息:如果你在Binsor上发布任何Q,我将能够帮助你前哨Sharp在执行AOP的机制方面更加全面。Autofac Castle core不支持IL代码编织,因此您将无法记录内部方法调用!由于PostSharp是开源的,只要你依赖它,开发就可以/将继续进行。理论上是这样,但不幸的是,理论和实践并不总是一样的。NDoc是开源的,曾经繁荣一时,但现在处于休眠状态。开源项目仍然需要一个社区围绕着它们,如果你依赖它,那么你可能会被留下来维护它。你想要那个负担吗?
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);
    }
}
}