3.2. Logging Activity

The iBATIS DataAccess framework records its interaction with the database through an internal logging mechanism patterned after Apache Log4Net. The internal logging mechanism can use one of the three built-in loggers (NoOpLogger, ConsoleOutLogger, TraceLogger) or external logging packages such as Apache Log4Net. In order for iBATIS to generate log messages, the application's config file (App.Config or Web.Config) must contain an appropriate configSection node:

Example 3.1. iBATIS Configuration Section Handler for logging

<configSections>
 <sectionGroup name="iBATIS">
  <section name="logging" type="IBatisNet.Common.Logging.ConfigurationSectionHandler, IBatisNet.Common" />
 </sectionGroup>
</configSections>

The application's config file must declare one logger implementation. See the examples below on how to configure one of the three built-in loggers.

<iBATIS>
 <logging>
  <logFactoryAdapter type="IBatisNet.Common.Logging.Impl.ConsoleOutLoggerFA, IBatisNet.Common">
   <arg key="showLogName" value="true" />
   <arg key="showDataTime" value="true" />
   <arg key="level" value="ALL" />
   <arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:SSS" />
  </logFactoryAdapter>
 </logging>
</iBATIS>
<iBATIS>
 <logging>
  <logFactoryAdapter type="IBatisNet.Common.Logging.Impl.NoOpLoggerFA, IBatisNet.Common" />
 </logging>
</iBATIS>
<iBATIS>
 <logging>
  <logFactoryAdapter type="IBatisNet.Common.Logging.Impl.TraceLoggerFA, IBatisNet.Common" />
 </logging>
</iBATIS>

To configure iBATIS to use another logger implementation, simple specify the appropriate logFactoryAdapter type. To use Apache Log4Net with the iBATIS framework, use the following configuration setting:

<iBATIS>
 <logging>
  <logFactoryAdapter type="IBatisNet.Common.Logging.Impl.Log4NetLoggerFA, IBatisNet.Common.Logging.Log4Net">
   <arg key="configType" value="inline" />
  </logFactoryAdapter>
 </logging>
</iBATIS>
<iBATIS>
 <logging>
  <logFactoryAdapter type="IBatisNet.Common.Logging.Impl.Log4NetLoggerFA, IBatisNet.Common.Logging.Log4Net">
   <arg key="configType" value="file" />
   <arg key="configFile" value="log4Net.config" />
  </logFactoryAdapter>
 </logging>
</iBATIS>

The Log4NetLoggerFA supports the following values for the configTypes argument:

Table 3.4. Valid configType values

configTypeDescription
inlinelog4net node will use the log4net node in the App.Config/Web.Config file when it is configured
file(also requires configFile argument) - log4net will use an external file for its configuration
file-watch (also requires configFile argument) - log4net will use an external file for its configuration and will re-configure itself if this file changes
externaliBATIS will not attempt to configure log4net.


3.2.1. Sample Logging Configurations

The simplest logging configuration is to output log messages to Console.Out:

<configuration>
 <configSections>
  <sectionGroup name="iBATIS">
   <section name="logging" type="IBatisNet.Common.Logging.ConfigurationSectionHandler, IBatisNet.Common" />
  </sectionGroup>
 </configSections>
 <iBATIS>
  <logging>
   <logFactoryAdapter type="IBatisNet.Common.Logging.Impl.ConsoleLoggerFA, IBatisNet.Common.Logging" />
  </logging>
 </iBATIS>
</configuration>

A common logging configuration is to use Apache Log4Net. To use Log4Net with your own application, you need to provide your own Log4Net configuration. You can do this by adding a configuration file for your assembly that includes a <log4Net> element. The configuration file is named after your assembly but adds a .config extension, and is stored in the same folder as your assembly. This is an example of a basic Log4Net configuration block (IBatisNet.DataMapper.Test.dll.Config) that also creates a log4net.txt which contains debug information from log4net. If log4net is not producing output, check the log4net.txt file.

<configuration>
 <configSections>
  <sectionGroup name="iBATIS">
   <section name="logging" type="IBatisNet.Common.Logging.ConfigurationSectionHandler, IBatisNet.Common" />
  </sectionGroup>
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
 </configSections>
 <appSettings>
  <add key="log4net.Internal.Debug" value="true"/>
 </appSettings>
 <system.diagnostics>
 <trace autoflush="true">
   <listeners>
    <add name="textWriterTraceListener" 
     type="System.Diagnostics.TextWriterTraceListener"
     initializeData="C:\\inetpub\\wwwroot\\log4net.txt" />
   </listeners>
  </trace>
 </system.diagnostics>
 <iBATIS>
  <logging>
   <logFactoryAdapter type="IBatisNet.Common.Logging.Impl.Log4NetLoggerFA, IBatisNet.Common.Logging.Log4Net">
    <arg key="configType" value="inline" />
   </logFactoryAdapter>
  </logging>
 </iBATIS>
 <log4net>
  <appender name="FileAppender" type="log4net.Appender.FileAppender">
   <file value="log.txt" />
   <appendToFile value="true" />
   <layout type="log4net.Layout.SimpleLayout" />
  </appender>
  <root>
   <level value="ALL" />
   <appender-ref ref="FileAppender" />
  </root>
 </log4net>
</configuration>