Spring uses a simple logging abstraction in order to provide a layer of indirection between logging calls made by Sprng and the specific logging library used in your application (log4net, EntLib logging, NLog). Many other .NET projects have done a similar task. As such this library is to be moved out of the Spring project and into a more general open source project. Stay tuned for futher information.
This logging abstraction goes by the name 'Common.Logging' and is simply a more packaged version of the logging abstraction used inside the iBATIS project. Many thanks to them! The library is available for .NET 1.0, 1.1, and 2.0 with both debug and strongly signed assemblies.
Spring ships with the base logging library, Common.Logging, that provides console and trace based loggers. The libraries are located under lib/logging. There are two enterprise logging implementations, one for log4net 1.2.9 and another for log4net 1.2.10. The need for two log4net versions is due to the fact that each is signed with a different strong key making assembly redirection impossible.
Note that it is not the intention of this library to be a replacement for the many fine logging libraries that are out there. The API is incredibly minimal and will very likely stay that way. Only use this library if you truely need to support multiple logging APIs.
The API is very simple
public interface ILog
{
void Debug( object message );
void Debug( object message, Exception exception );
void Error( object message );
void Error( object message, Exception exception );
void Fatal( object message );
void Fatal( object message, Exception exception );
void Info( object message );
void Info( object message, Exception exception );
void Warn( object message );
void Warn( object message, Exception exception );
bool IsDebugEnabled { get; }
bool IsErrorEnabled { get; }
bool IsFatalEnabled { get; }
bool IsInfoEnabled { get; }
bool IsWarnEnabled { get; }
}You can get an reference to an instance of an ILog using the LoggingManager class. Its API is shown below
public sealed class LogManager
{
public static ILog GetLogger( Type type ) ...
public static ILog GetLogger( string name ) ...
public static ILoggerFactoryAdapter Adapter ...
}
The Adapter property is used by the framework itself.
Calling code looks typically like this
There are simple out of the box implementations and a log4net one.
The base library, Common.Logging, contains a console out logger. Is configured in the following manner
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging">
<arg key="showLogName" value="true" />
<arg key="showDataTime" value="true" />
<arg key="level" value="DEBUG" />
<arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:fff" />
</factoryAdapter>
</logging>
</common>
There are two implementations, both configured similarly. The only difference in in the type specified to the factory adapter. The one for log4net 1.2.10 is shown below
<common>
<logging>
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
<!-- choices are INLINE, FILE, FILE-WATCH, EXTERNAL-->
<!-- otherwise BasicConfigurer.Configure is used -->
<!-- log4net configuration file is specified with key configFile-->
<arg key="configType" value="INLINE" />
</factoryAdapter>
</logging>
</common>
For log4net 1.2.9 change the assembly name Common.Logging.Log4Net129