views:

26

answers:

1

In the castle stack?

after the answer I came up with this:

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

out of the box? No I don't think so. It's trivial to do with custom ComponentModel construction contributor though.

Krzysztof Koźmic
@red Krzysztof: Ok with that impl, or is there some problem with that?
Jan Limpens

related questions