Skip to content

Commit

Permalink
Update documentation to reflect changes to logging facility
Browse files Browse the repository at this point in the history
  • Loading branch information
Jevonius committed Jan 22, 2023
1 parent c42781e commit f021840
Showing 1 changed file with 40 additions and 31 deletions.
71 changes: 40 additions & 31 deletions docs/logging-facility.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,65 @@ Castle Core [provides many logger abstraction implementations](https://github.co

## Registering the facility

:warning: **`LoggerImplementation` enum and the `loggingApi` XML property are deprecated:** Usage of `LogUsing` and `customLoggerFactory` are highly recommended even for Castle Core provided implementations.
### In code

The recommended way of configuring the facility is using code. When specifying custom `ILoggerFactory` or `IExtendedLoggerFactory` you use the following generic overload:

```csharp
container.AddFacility<LoggingFacility>(f => f.LogUsing<CustomLoggerFactory>());
```

For example, using the log4net logger factory with configuration stored in a `log4net.xml` file, the code would look like this:

```csharp
container.AddFacility<LoggingFacility>(f => f.LogUsing<Log4netFactory>().WithConfig("log4net.xml"));
```

#### Built-in logging factories

There are a few helper methods for built-in logging factories:
```csharp
// Null Logger
container.AddFacility<LoggingFacility>(f => f.LogUsingNullLogger());

// Console Logger
container.AddFacility<LoggingFacility>(f => f.LogUsingConsoleLogger());

// Diagnostics Logger
container.AddFacility<LoggingFacility>(f => f.LogUsingDiagnosticsLogger());

// Trace Logger
container.AddFacility<LoggingFacility>(f => f.LogUsingTraceLogger());
```

### Via XML Configuration

Logging facility exposes minimalistic configuration:
It is also possible to configure the facility via XML. For example the same configuration for log4net as above:

```xml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<facility
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" />
customLoggerFactory="Castle.Services.Logging.Log4netIntegration.Log4netFactory, Castle.Services.Logging.Log4netIntegration"
configFile="log4net.xml" />
</configuration>
```

For example to use log4net with logger configuration stored in `log4net.xml` file, you would configure the facility like this:
The full list of configuraation attributes is shown in the following example:

```xml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<facility
type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging"
loggingApi="log4net"
configFile="log4net.xml" />
customLoggerFactory="<type of factory>"
configFile="<path to configuration file (optional attribute)>"
loggerLevel="<the loggerLevel (optional attribute)>"
configuredExternally="<boolean value (optional attribute)>" "/>
</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 look like this:

```csharp
container.AddFacility<LoggingFacility>(f => f.LogUsing<Log4netFactory>().WithConfig("log4net.xml"));
```

When specifying custom `ILoggerFactory` or `IExtendedLoggerFactory` you use the following generic overload:

```csharp
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:
Expand All @@ -66,17 +81,11 @@ using Castle.Core.Logging;
public class CustomerService
{
private ILogger logger = NullLogger.Instance;

public CustomerService()
{
}
public ILogger Logger
{
get { return logger; }
set { logger = value; }
}
public ILogger Logger { get; set; } = NullLogger.Instance;
// ...
}
Expand All @@ -87,4 +96,4 @@ With the approach above, the logger field will never be null. Also, if the loggi
## Required Assemblies
* `Castle.Facilities.Logging.dll` (bundled with Windsor)
* `Castle.Core.dll` (contains the `ILogger` and `ILoggerFactory`)
* `Castle.Core.dll` (contains the `ILogger` and `ILoggerFactory` interfaces; included as a dependency in the Windsor NuGet package)

0 comments on commit f021840

Please sign in to comment.