Table of contents
Logging Facility
The Logging Facility provides custom loggers for your components.
| Facility Summary | |||||||||||
|
| ||||||||||
Introduction
The logging facility provides a seemless 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.
Loggers
You can use the following loggers implementations:
- log4net (requires Castle.Services.Logging.Log4netIntegration.dll)
- NLog (requires Castle.Services.Logging.NLogIntegration.dll)
- ConsoleLogger
- DiagnosticsLogger
- StreamLogger
- WebLogger (TraceContext)
- NullLogger (used as placeholder)
Quick start
Your first step is to add the Logging Facility into your Windsor container, either by using the configuration schema listed below, or in code.
<?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>
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
- Castle.Core.dll (contains the ILogger and ILoggerFactory)