RSS

导航







快速搜索

高级搜索 »

注意:此页面是Spring.NET框架 1.1 参考文档的一部分。


编辑

13.1. 简介

Spring.NET用一组简单的抽象类库在具体的日志API(如log4net,EntLib logging,NLog)和Spring.NET的日志调用之间形成了一个间接层。很多.NET项目也都做了类似的工作,所以我们将相关的类库移出了 Spring.NET,创建了一个更为通用的开源项目。请随时留意这方面的信息。

Spring.NET的日志抽象层名为“Common.Logging”,它来自iBATIS项目,算是iBATIS日志抽象的一个紧凑版本。在此十分感谢iBATIS!该类库适用于.NET 1.0、1.1和2.0,支持调试功能和强命名程序集。

Common.Logging.dll是随Spring.NET一起发布的,支持基于控制台和Trace的Logger。该程序集位于lib文件夹下,且分别针对log4net 1.2.9和log4net1.2.10有两个版本的实现。这么做是因为log4net这两个版本的程序集都使用强命名,不能在运行时重定向。

注意Common.Logging的目的不是要取代现有的其它日志类库。它的API相当简陋,将来也不打算做的更复杂。该类库的惟一用途是解决同时使用多种日志API的需求。

顶部

编辑

13.1.1. Logging API

API相当的简单:

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; }
}

可以用LogManager类获取ILog的引用。LogManager定义如下:

public sealed class LogManager
{
 public static ILog GetLogger( Type type ) ... 
 public static ILog GetLogger( string name ) ...

 public static ILoggerFactoryAdapter Adapter ...

}

其中的Adapter属性由框架本身使用。

调用代码基本上如下所示:

顶部

编辑

13.2. 实现与配置

目前的实现都很简单,包括一个基于log4net的实现。

顶部

编辑

13.2.1.控制台Logger

基础类库Common.Logging包含一个控制台输出Logger,可以用以下方式配置:

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

顶部

编辑

13.3. Log4Net

基于log4net的实现有两个版本,它们的配置很相似,惟一的区别是指定给工厂适配器的类型。基于log4net 1.2.10的实现是这样:

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

如果用log4net 1.2.9,要把程序集名称改为Common.Logging.Log4Net129。

顶部

.NET 藏经阁 | | 版权所有 ©2008 entlib.net.cn