いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

iBatis.NET奮闘記-008 (ログの設定) (ConfigurationSectionHandler, Log4NetConfigurationSectionHandler, ConsoleOutLoggerFA, Log4NetLoggerFA)


今回は、iBatis.NETのログの設定を行ってみます。

iBatis.NETには、組み込みのロガーとLog4Netを用いたロギングの2つの方法があります。


組み込みとして用意されているロガーは以下のものがあります。

  • ConsoleOutLoggerFA
  • NoOpLoggerFA
  • TraceLoggerFA

名前を見たらそのままですが、ConsoleOutLoggerFAはコンソールにログを、NoOpLoggerFAは何もしない、TraceLoggerFAは
System.Diagnostics.Traceを用いてログを出力します。


以下は、ConsoleOutLoggerFAの設定例です。iBatis.NETでは独自のSectionGroupを定義する必要があります。
以下のようにします.(ドキュメントの設定例そのままですが・・・・w)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    
    <!--
        iBatis.NETのログ設定 (基本設定)
    -->
    <configSections>
        <sectionGroup name="iBATIS">
            <section name="logging" type="IBatisNet.Common.Logging.ConfigurationSectionHandler, IBatisNet.Common" />
        </sectionGroup>
    </configSections>
    
    <!--
        ConsoleOutLoggerFAの設定.
    -->
    <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>

</configuration>

Levelを変えることで、そのレベルに応じたログが出力されます。
DEBUGにしておくと、SQLも見れるので便利です。


んで、次はLog4Netを用いた設定方法です。実際の実務ではこちらの方が多いとおもいます。
基本的な設定は同じで、その上にLog4Netのロガーを使用するように設定します。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    
    <!--
        iBatis.NETのログ設定 (基本設定)
    -->
    <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="false"/>
    </appSettings>

    <!--
        iBatis.NETのLog4Net用のロガー設定.
    -->
    <iBATIS>
        <logging>
            <logFactoryAdapter type="IBatisNet.Common.Logging.Impl.Log4NetLoggerFA, IBatisNet.Common.Logging.Log4Net">
                <arg key="configType" value="inline" />
            </logFactoryAdapter>
        </logging>
    </iBATIS>

    <!--
        Log4Netの設定
    -->
    <log4net>
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
            <layout type="log4net.Layout.PatternLayout">
                <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n" />
            </layout>
        </appender>
        <root>
            <level value="DEBUG" />
            <appender-ref ref="ConsoleAppender" />
        </root>
    </log4net>

</configuration>

上記のように設定すると、例えば前回の実行結果などが以下のように出力されます。
以下は、ConsoleOutLoggerFAの出力結果です。

2007/12/03 20:12:53:SSS [DEBUG] IBatisNet.Common.Utilities.ConfigWatcherHandler - Adding file [SqlMap.config] to list of watched files.
2007/12/03 20:12:53:SSS [DEBUG] IBatisNet.DataMapper.Configuration.DomSqlMapBuilder - Add property "datasource" value ".\SQLEXPRESS"
2007/12/03 20:12:53:SSS [DEBUG] IBatisNet.DataMapper.Configuration.DomSqlMapBuilder - Add property "datasourceName" value "IBatisNetSampleDB"
2007/12/03 20:12:53:SSS [DEBUG] IBatisNet.DataMapper.Configuration.DomSqlMapBuilder - Add property "database" value "ibatisnet_sample_db"
2007/12/03 20:12:53:SSS [DEBUG] IBatisNet.DataMapper.Configuration.DomSqlMapBuilder - Add property "integratedSecurity" value "True"
2007/12/03 20:12:53:SSS [DEBUG] IBatisNet.DataMapper.Configuration.DomSqlMapBuilder - Add property "provider" value "sqlServer2.0"
2007/12/03 20:12:54:SSS [DEBUG] IBatisNet.DataMapper.Configuration.Statements.PreparedStatementFactory - Statement Id: [MemoAndAuthor.FindMemoAndAuthor] Prepared SQL: [select                        m.MemoId    as MemoId                       ,a.AuthorId  as AuthorId                       ,a.Name      as AuthorName                       ,a.Age       as AuthorAge                       ,m.Title     as MemoTitle                       ,m.MemoData  as MemoData                  from                       Authors a                      ,Memos   m                  where                      m.MemoId   =  @param0                       and                      m.AuthorId = a.AuthorId]
2007/12/03 20:12:54:SSS [DEBUG] IBatisNet.DataMapper.Commands.DefaultPreparedCommand - Statement Id: [MemoAndAuthor.FindMemoAndAuthor] PreparedStatement : [select                        m.MemoId    as MemoId                       ,a.AuthorId  as AuthorId                       ,a.Name      as AuthorName                       ,a.Age       as AuthorAge                       ,m.Title     as MemoTitle                       ,m.MemoData  as MemoData                  from                       Authors a                      ,Memos   m                  where                      m.MemoId   =  @param0                       and                      m.AuthorId = a.AuthorId]
2007/12/03 20:12:54:SSS [DEBUG] IBatisNet.DataMapper.Commands.DefaultPreparedCommand - Statement Id: [MemoAndAuthor.FindMemoAndAuthor] Parameters: [@param0=[value,1]]
2007/12/03 20:12:54:SSS [DEBUG] IBatisNet.DataMapper.Commands.DefaultPreparedCommand - Statement Id: [MemoAndAuthor.FindMemoAndAuthor] Types: [@param0=[Int32, System.Int32]]
2007/12/03 20:12:54:SSS [DEBUG] IBatisNet.DataMapper.SqlMapSession - Open Connection "57570381" to "Microsoft SQL Server, provider V2.0.0.0 in framework .NET V2.0".
2007/12/03 20:12:54:SSS [DEBUG] IBatisNet.DataMapper.SqlMapSession - Close Connection "57570381" to "Microsoft SQL Server, provider V2.0.0.0 in framework .NET V2.0".
memo-id:1, author-[id:1, name:gsf_zero1, age:28], title:Title-001, data:Memo-001