Note that there are some explanatory texts on larger screens.

plurals
  1. POWindsor Logging Facility: Control log name
    primarykey
    data
    text
    <p>I'm working on adding logging to a project using Windsor Logging Facility and the NLog integration.</p> <p>Rather than following the Windsor documentation's recommended practice of adding a <code>Log</code> 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 <code>ILogger</code>:</p> <pre><code>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(); } } </code></pre> <p>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.</p> <pre><code>container.Register(Component.For&lt;LoggingInterceptor&gt;().LifeStyle.Transient); container.Register( Component.For&lt;IEmployeeRepository&gt;() .ImplementedBy&lt;EmployeeRepository&gt;() .Interceptors(InterceptorReference.ForType&lt;LoggingInterceptor&gt;()).First); </code></pre> <p>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.</p> <p>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 <code>IEmployeeRepository</code> then the log should be named <code>EmployeeRepository</code>. Is this doable?</p> <p><strong>Edit:</strong> I've tried implementing a custom <code>ILoggerFactory</code> 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 <code>ILoggerFactory</code> has no way of finding out about the abstraction the interceptor has been applied to.</p> <p>I notice that there are two overloads of <code>ILoggerFactory.Create()</code> 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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload