Welcome Guest, you are in: Login

Castle Project

RSS RSS

Navigation (Windsor)






Search the wiki
»

PoweredBy

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

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

Introduction

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.

Image
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.

Loggers

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
ConsoleLogger
DiagnosticsLoggernot available in Silverlight version
StreamLogger
NullLogger used as placeholder

Registering the facility

Via XML configuration

Logging facility exposes minimalistic configuration:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration>
   <facility 
      id="logging"
      type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging"
      loggingApi="null|console|diagnostics|web|nlog|log4net|custom"
      customLoggerFactory="type name that implements ILoggerFactory"
      configFile="optional config file location" />
</configuration>

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" ?> 
<configuration>
   <facility 
      id="logging"
      type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging"
      loggingApi="log4net"
      configFile="log4net.xml" />
</configuration>

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 3.0.4.560. Some of the icons created by FamFamFam.