Welcome Guest, you are in: Login

Castle Project


Navigation (Windsor)

Search the wiki


This documentation is a work in progress.

Let us know if some information is not clear, inaccurate or missing. Also feel free to update the wiki yourself.

Logging Facility

Modified on 2014/11/05 05:39 by Jonathon Rossi Categorized as Facilities


The logging facility provides a seamless way to add logging capabilities to your application. There are two levels of integration.
  • Allow your classes to receive an ILogger instance for logging support
  • Allow you to ask for an ILoggerFactory instance to provide logging support to classes that are not managed by Windsor.

Logger abstraction

Castle project does not contain its own logging framework. There are already excellent frameworks out there. Instead ILogger and ILoggerFactory are abstractions Windsor uses to decouple itself from the framework you decide to use.


You can use the following loggers implementations:

logger nameDescriptionNotes
log4netApache log4net frameworkrequires Castle.Services.Logging.Log4netIntegration.dll
NLogNLog framework (version 2.0)requires Castle.Services.Logging.NLogIntegration.dll
SerilogSerilog framework (version 1.3.33)requires Castle.Services.Logging.SerilogIntegration.dll
DiagnosticsLoggernot available in Silverlight version
NullLogger used as placeholder

Registering the facility

Via XML configuration

Logging facility exposes minimalistic configuration:

<?xml version="1.0" encoding="utf-8" ?> 
      type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging"
      customLoggerFactory="type name that implements ILoggerFactory"
      configFile="optional config file location" />

For example to use log4net with logger configuration stored in log4net.xml file, you would configure the facility like this:

<?xml version="1.0" encoding="utf-8" ?> 
      type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging"
      configFile="log4net.xml" />

In code

Recommended way of configuring the facility however, is using code. The facility exposes the same options like via XML. For example the same configuration for log4net as above, from code would like like this.

container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.Log4net).WithConfig("log4net.xml"));

When specifying custom ILoggerFactory or IExtendedLoggerFactory you use the following generic overload:
container.AddFacility<LoggingFacility>(f => f.LogUsing<CustomLoggerFactory>());

Best practices

We recommend that you make logging optional on your components/services. This way you maximize the reusability. For example:

using Castle.Core.Logging;
public class CustomerService
   private ILogger logger = NullLogger.Instance;

   public CustomerService()
   public ILogger Logger
      get { return logger; }
      set { logger = value; }

   // ...

With the approach above, the logger field will never be null. Also, if the logging facility was registered on the container, it will be able to supply a logger instance using the Logger property.

Required Assemblies

  • Castle.Facilities.Logging.dll (bundled with Windsor)
  • Castle.Core.dll (contains the ILogger and ILoggerFactory)

ScrewTurn Wiki version Some of the icons created by FamFamFam.