Castle Windsor NLog Integration


Castle Windsor comes with what it calls a “Logging Facility“. What the latter allows you to do is very quickly enable logging using a variety of ways but what is most interesting is the fact that the “Logging Facility” provides the necessary plumbing for swapping one type of logger for another. So for instance, you can change from log4net to NLog very easily by simply changing one line of code.

In fact, that’s what I had to do i.e. swap out log4net for NLog because I had started logging using the former but then found out that the latter might possibly be better.

While there are examples of how to use the “Logging Facility” with log4net, I haven’t found any with dealt specifically with NLog integration and Castle Windsor in detail. While the resources provided earlier do help and can be used to figure out how to get NLog integration, the information needed aggregating which this post attempts to address.

Set up Castle Windsor NLog integration

This is very straighforward if you use Nuget. Simply add the package “Castle Windsor NLog” to your project.

Castle Windsor Nlog IntegrationAlso via Nuget install both “NLog Configuration” and “NLog Schema for Intellisense(TM)” to your project.

NLog config and NLog Schema

The former is how you will configure NLog i.e. where to create the log file, etc while the latter will provide intellisense while editing the configuration file.

So here’s how you might want to configure logging:

The next step is to ensure that CastleWindsor will use NLog to perform logging:

container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.NLog)

If let’s say you want to use log4net instead then instead simply use (assuming you’ve got the “” present):

container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.Log4net)                                                         

Now according to best practice advice to set up logging:

Finally, log whatever you want e.g.:

I’ve put together the source code here.

Posted in .NET, ALT.NET, Castle.Windsor, IoC, NLog
4 comments on “Castle Windsor NLog Integration
  1. tomarikawa says:

    I’m a newbie in IoC-Frameworks. I’m trying to put up a demo-implementation similar to above example. Could I have more code about the calling class and its instantiation, please?

    Somehow I’m missing the point: If I just create a class (called MyBiz) with the Logger-properties and instantiated it, the logger-property-value remains NullLogger and IsLevel-properites are false. Does The setting Logger-property need to be done explicitely or does Castle this for me? If yes, Then I suppose, the MyBiz needs to be somehow “registered” to. How then?

    • DavidS says:

      Hi Tomarikawa,

      I will put up an example on GitHub and you will be able to see how it work from there. However, while waiting ensure you’ve got:

      container.AddFacility(f => f.LogUsing(LoggerImplementation.NLog)

      in your bootstrap code.

      After I put up the example you’ll see better how it works.


  2. DavidS says:

    Hi Tomarikawa,

    Here is an example I’ve put together This should hopefully help you. Give me a shout if you have any issue. Also note that the code I’ve got is not how I would normally do things because everything is currently in one file . I’ve done it that way to keep things simple. Anyway, let me know how you get on. Oh and in case you’re wondering, the log file will be in bin/Debug/logs.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: