Logging with Spring in .NET
Introduction
To avoid introducing a dependency on a particular logging framework Spring uses it's own logging abstraction: the "Common.Logging" library. This library is based on work done by the developers of IBatis.NET and it's usage is inspired by log4net. It provides an easy mechanism to plug in any logging framework using FactoryAdapters.
Configuring Logging
Declarative Configuration
Logging configuration can be done declaratively in your app.config:
<configuration>
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Simple.TraceLoggerFactoryAdapter, Common.Logging">
<arg key="showDateTime" value="true" />
</factoryAdapter>
</logging>
</common>
</configuration>
The concrete set of <arg> elements you may specify depend on the FactoryAdapter being used.
Configuring Logging in your code
You may manually configure logging by setting a LoggerFactoryAdapter in your code:
// create properties
NameValueCollection properties = new NameValueCollection();
properties["showDateTime"] = "true";
// set Adapter
Common.Logging.LogManager.Adapter =
new Common.Logging.Simple.TraceLoggerFactoryAdapter(properties);
The concrete set of properties you may specify depend on the FactoryAdapter being used.
Using Common.Logging API in your own code
Usage of the Logging API is fairly simple. You need to obtain a logger from the LogManager and call the appropriate logging method:
using Common.Logging;
...
ILog log = LogManager.GetLogger(this.GetType());
log.Debug("hello world");
It is also possible to obtain a logger by name:
ILog log = LogManager.GetLogger("mylogger");
The methods available for logging are:
- Debug()
- Info()
- Warn()
- Error()
- Fatal()
All methods are overloaded to support logging exceptions as well.
Since ILog interface mimics the look&feel of log4net, migration is just a matter of changing the "using" statement.
Posted in: programming asp.net | Tags: .net c# asp.net configuration logging spring configuring logging configsections sectiongroup configurationsectionhandler factoryadapterAdapters available in Common.Logging in .NET
All standard FactoryAdapters provided by the Common.Logging library accept the following optional configuration properties (use <arg> for declarative configuration):
| Key | Possible Value(s) | Description |
| level | All | Defines the global maximum level of logging. |
| showDateTime | true|false | output timestamp? |
| showLogName | true|false | output logger name? |
| dateTimeFormat | any formatstring accepted by DateTime.ToString() | defines the format to be used for output the timestamp. |
NoOpLoggerFactoryAdapter
This is the default FactoryAdapter if logging is not configured. It simply does nothing.
ConsoleOutLoggerFactoryAdapter
ConsoleOutLoggerFactoryAdapter uses Console.Out for logging output.
TraceLoggerFactoryAdapter
TraceLoggerFactoryAdapter uses System.Diagnostics.Trace for logging output.
Posted in: programming | Tags: c# spring adapters common.logging showdatetime showlogname datetimeformat nooploggerfactoryadapter consoleoutloggerfactoryadapter traceloggerfactoryadapter traceloggerfactoryadapterAdditional Adapters in Spring - log4net
As log4net is a widely using logging framework, there are 2 additional adapters for log4net:
- Common.Logging.Log4Net
is linked against log4net 1.2.10.0
- Common.Logging.Log4Net129
is linked against log4net 1.2.9.0
2 different Adapters are necessary because log4net unfortunately changed it's public key from 1.2.9.0 to 1.2.10.0 (which was the primary reason for introducing Common.Logging)
Both Adapters accept the following configuration properties:
| Key | Possible Value(s) | Description |
| configType | FILE | INLINE will simply call XmlConfigurator.Configure() EXTERNAL expects log4net being configured somewhere else in your code and does nothing. FILE, FILE-WATCH: see property "configFile" below. |
| configFile | <path to your log4net.config file> | if configType is FILE or FILE-WATCH, the value of "configFile" is passed to XmlConfigurator.Configure (FileInfo) / ConfigureAndWatch(FileInfo) method. |
Example:
<common>
<logging>
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net">
<arg key="configType" value="FILE-WATCH" />
<arg key="configFile" value="~/log4net.config" />
</factoryAdapter>
</logging>
</common>
will configure log4net 1.2.10.0 using the file log4net.config from your application's root directory by calling XmlConfigurator.ConfigureAndWatch().
Advanced Logging Tasks
Implementing your own FactoryAdapter
If you need to implement your own FactoryAdapter, you need to implement the Common.Logging.ILoggerFactoryAdapter interface. An implementation must provide a public constructor accepting a NameValueCollection parameter:
public class MyLoggingFactoryAdapter : ILoggerFactoryAdapter
{
public MyLoggingFactoryAdapter(NameValueCollection properties)
{
// configure according to properties
}
public ILog GetLogger(Type type) { ... }
public ILog GetLogger(string name) { ... }
}