0

在城堡堆栈中?

回答后我想出了这个:

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

1 回答 1

0

盒子外面?不,我不这么认为。不过,与自定义ComponentModel 构造贡献者有关是微不足道的。

于 2010-08-25T23:34:28.990 回答