是否存在Windsor / DynamicProxy日志记录方面,我可以将其作为属性放置到我的类/方法中?

时间:2010-08-25 22:24:39

标签: castle-windsor castle-dynamicproxy

在城堡堆里?

答案之后我想出了这个:

namespace Limpens.Windsor.LoggingAspect
{
    using System;
    using Castle.Core;
    using Castle.Core.Interceptor;
    using Castle.Core.Logging;
    using Castle.MicroKernel;
    using Castle.MicroKernel.ModelBuilder;

    public class LoggingContributor : IContributeComponentModelConstruction
    {
        public void ProcessModel(IKernel kernel, ComponentModel model)
        {
            model.Interceptors.Add(new InterceptorReference(typeof(LoggingInterceptor)));
        }
    }

    public class LoggingInterceptor : IInterceptor
{
    private const string Format = "HH:mm:ss:fff";
    private readonly ILogger _logger = NullLogger.Instance;

    public LoggingInterceptor(ILogger logger)
    {
        _logger = logger;
    }

    public int TresholdInMs { get; set; }

    #region IInterceptor Members

    public void Intercept(IInvocation invocation)
    {
        var start = DateTime.Now;
        invocation.Proceed();
        var finished = DateTime.Now;
        var duration = (finished - start).Milliseconds;

        if (duration < TresholdInMs) return;

        var typeName = invocation.TargetType.Name;
        var methodName = invocation.Method.Name;

        _logger.DebugFormat("{0}.{1} started at {2} and finished at {3}. Took {4} ms.",
                            typeName,
                            methodName,
                            start.ToString(Format),
                            finished.ToString(Format),
                            duration);
    }
}

1 个答案:

答案 0 :(得分:0)

开箱即用?不,我不这么认为。尽管如此,使用自定义ComponentModel construction contributor也是微不足道的。