在城堡堆栈中?
回答后我想出了这个:
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);
}
}