I'm working on adding logging to a project using Windsor Logging Facility and the NLog integration.
Rather than following the Windsor documentation's recommended practice of adding a Log
property to every class for which I want to support logging, I've decided to try to go the route of using dynamic interception to do it. So far the interceptor's fairly basic; it just uses constructor injection to get an instance of ILogger
:
class LoggingInterceptor : IInterceptor
{
private readonly ILogger _logger;
public LoggingInterceptor(ILogger logger)
{
if (logger == null) throw new ArgumentNullException("logger");
_logger = logger;
}
public void Intercept(IInvocation invocation)
{
// just a placeholder implementation, I'm not actually planning to do this ;)
_logger.Info(invocation.Method.Name);
invocation.Proceed();
}
}
Beyond that, all I've done is the minimum for registering the interceptor and applying it to a component, and the logging facility takes care of the rest.
container.Register(Component.For<LoggingInterceptor>().LifeStyle.Transient);
container.Register(
Component.For<IEmployeeRepository>()
.ImplementedBy<EmployeeRepository>()
.Interceptors(InterceptorReference.ForType<LoggingInterceptor>()).First);
So far so good - sort of. The logger I get follows NLog's recommended practice of one logger per class. I don't think that's a very good choice in this case. It means that every single message is going to a log named "MyApplication.Interceptors.LoggingInterceptor", which isn't terribly useful.
I'd prefer to have logs named after the abstraction the logger has been applied to. For example, if the logger has been applied to an implementation of IEmployeeRepository
then the log should be named EmployeeRepository
. Is this doable?
Edit:
I've tried implementing a custom ILoggerFactory
and instructing the container to use it instead. However I quickly hit a roadblock: When Windsor calls into the factory, the only information supplied is the type of the object for which the logger is being acquired. No other information about the object is supplied, so the ILoggerFactory
has no way of finding out about the abstraction the interceptor has been applied to.
I notice that there are two overloads of ILoggerFactory.Create()
that accept strings as arguments. Windsor doesn't seem to be using either of them directly, but one would assume that they have to be there for a reason. Is there anything in the fluent registration API that can be used to specify that a particular string be used?
Here is a question that is very similar to yours and it has an accepted answer. In the link, the person posting the answer suggests making the interceptor dependent on ILoggerFactory and in the Intercept method, use the IInvocation.TargetType as the type to send to the ILoggerFactory to get the appropriate logger.
Note, I don't use Castle, so I can't comment much more on this suggestion.
public class LoggingInterceptor : IInterceptor
{
private readonly ILoggerFactory _loggerFactory;
public LoggingInterceptor(ILoggerFactory loggerFactory)
{
_loggerFactory = loggerFactory;
}
public void Intercept(IInvocation invocation)
{
var logger = _loggerFactory.Create(invocation.TargetType);
if (logger.IsDebugEnabled)
{
logger.Debug(CreateInvocationLogString(invocation));
}
try
{
invocation.Proceed();
}
catch (Exception e)
{
logger.Warn(CreateInvocationLogString(invocation));
throw;
}
logger.Info(CreateInvocationLogString(invocation));
}
private static String CreateInvocationLogString(IInvocation invocation)
{
var sb = new StringBuilder(100);
sb.AppendFormat("Called: {0}.{1}(", invocation.TargetType.Name, invocation.Method.Name);
foreach (var argument in invocation.Arguments)
{
var argumentDescription = argument == null ? "null" : argument.ToString();
sb.Append(argumentDescription).Append(",");
}
if (invocation.Arguments.Any())
{
sb.Length--;
}
sb.Append(")");
return sb.ToString();
}
}
Castle: How can i get the correct ILogger in the logging interceptor?
You have probably already seen this, but here is a link to a Castle example that shows how to create a logging interceptor and how, apparently, to create a logger for the wrapped type:
http://docs.castleproject.org/Windsor.Introduction-to-AOP-With-Castle.ashx
EDIT: The implementation of ILoggerFactory for nlog is ExtendedNLogFactory, it's in the Castle.Core-NLog nuget package (http://www.nuget.org/packages/Castle.Core-NLog)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With