Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8984725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:10:36+00:00 2026-06-15T21:10:36+00:00

I’m working on adding logging to a project using Windsor Logging Facility and the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:10:37+00:00Added an answer on June 15, 2026 at 9:10 pm

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.