IBatis vs Log4Net

672 views
Skip to first unread message

JKT

unread,
Jun 12, 2012, 6:13:41 AM6/12/12
to mybatis...@googlegroups.com
Hi,

I want to log the queries executed by IBatis using log4Net in  a file. But tried everything but log4net is not working for me. Little help will be really appreciated

This my project configuration
 app.config
<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="iBATIS">
      <!--<section name="logging" type="IBatisNet.Common.Logging.ConfigurationSectionHandler, IBatisNet.Common" />-->
      <section name="logging" type="IBatisNet.Common.Logging.ConfigurationSectionHandler, IBatisNet.Common, Version=1.6.1.0, Culture=Neutral, PublicKeyToken=ed781d9fc396c6ca" />
    </sectionGroup>
    <!--<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />-->
  </configSections>
  <iBATIS>
    <logging>
      <logFactoryAdapter type="IBatisNet.Common.Logging.Impl.Log4NetLoggerFA, IBatisNet.Common.Logging.Log4Net, Version=1.6.1.0, Culture=Neutral, PublicKeyToken=ed781d9fc396c6ca">
        <arg key="configType" value="file" />
        <arg key="configFile" value="log4Net.config" />
      </logFactoryAdapter>
    </logging>
  </iBATIS>
 
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net"
             type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=1.2.10.0, Culture=Neutral, PublicKeyToken=bf100aa01a5c2784" />
  </configSections>
  <log4net debug="true">
    <appender name="RollingLogFileAppender"
              type="log4net.Appender.RollingFileAppender, log4net, Version=1.2.10.0, Culture=Neutral, PublicKeyToken=bf100aa01a5c2784" >
      <file value="ibat.txt" />
      <param name="MaxSizeRollBackups" value="2" />
      <param name="MaximumFileSize" value="1000KB" />
      <param name="RollingStyle" value="Size" />
      <param name="StaticLogFileName" value="true" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d - %m%n" />
      </layout>
    </appender>
    <!-- iBatis internal logging -->
    <logger name="IBatisNet.DataMapper">
      <level value="DEBUG" />
    </logger>
    <logger name="IBatisNet.DataMapper.Commands.DefaultPreparedCommand">
      <level value="DEBUG" />
    </logger>
    <logger name="IBatisNet.DataMapper.Configuration.Cache.CacheModel">
      <level value="DEBUG" />
    </logger>
    <logger name="IBatisNet.DataMapper.LazyLoadList">
      <level value="DEBUG" />
    </logger>
  </log4net>
</configuration>

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBatisNet.DataMapper;
using IBatisNet.Common;
using IBatisNet.Common.Logging;
using IBatisNet.Common.Logging.Impl;
using log4net;

namespace ConsoleApplication1
{
    public class Program
    {
        public static ISqlMapper myMapper
        {
            get
            {
                try
                {
                    ISqlMapper mapper = Mapper.Instance();
                    mapper.DataSource.ConnectionString = "Data Source=(local);Initial Catalog=testDb;Integrated Security=True";
                    return mapper;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        public static Guid executeFunction()
        {
           
            ISqlMapper mapper = myMapper;
            Guid str = mapper.QueryForObject<Guid>("FindId", "104569");
            return str;

        }
  
        static void Main(string[] args)
        {          

            Console.Write(executeFunction());
            Console.Read();
        }
    }
}



Ron Grabowski

unread,
Jun 12, 2012, 5:30:52 PM6/12/12
to mybatis...@googlegroups.com
  <iBATIS>
    <logging>
      <logFactoryAdapter type="IBatisNet.Common.Logging.Impl.Log4NetLoggerFA, IBatisNet.Common.Logging.Log4Net">
        <arg key="configType" value="external" />
      </logFactoryAdapter>
    </logging>
  </iBATIS>

Now you can initialize logging from your application and let ibatis piggy back on that repository:

XmlConfigurator.ConfigureAndWatch(
                new FileInfo(
                    AppDomain.CurrentDomain.SetupInformation.ApplicationBase +
                    "log4net.config"));


From: JKT <jatin...@gmail.com>
To: mybatis...@googlegroups.com
Sent: Tuesday, June 12, 2012 6:13 AM
Subject: IBatis vs Log4Net

JKT

unread,
Jun 13, 2012, 7:02:48 AM6/13/12
to mybatis...@googlegroups.com, Ron Grabowski
Thank you for reply and
Doing the change you have mentioned log file did started to be created in my project, however it is not logging any details like sql executed and parameter passed

following are the changes to my files
program.cs
 static void Main(string[] args)
        {
            XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "log4net.config"));

            Console.Write(executeFunction());
            Console.Read();
        }

the log files contains
[Header]
[Footer]
[Header]
[Footer]


where i want some thing like this
2012-06-13 01:39:33 [3872] DEBUG IBatisNet.DataMapper.Commands.DefaultPreparedCommand -
Statement Id: [Page.Select] PreparedStatement : [Select PageId From Pages WHERE [PageName] = ?]
2012-06-13 01:39:43 [3872] DEBUG IBatisNet.DataMapper.Commands.DefaultPreparedCommand -
Statement Id: [Page.Select] Parameters: [param0=[PageName,TestiBatis]]
2012-06-13 01:39:53 [3872] DEBUG IBatisNet.DataMapper.Commands.DefaultPreparedCommand -
Statement Id: [Page.Select] Types: [param0=[String, System.Int32]]








On Wednesday, 13 June 2012 03:00:52 UTC+5:30, Ron Grabowski wrote:
  <iBATIS>
    <logging>
      <logFactoryAdapter type="IBatisNet.Common.Logging.Impl.Log4NetLoggerFA, IBatisNet.Common.Logging.Log4Net">
        <arg key="configType" value="external" />
      </logFactoryAdapter>
    </logging>
  </iBATIS>

Now you can initialize logging from your application and let ibatis piggy back on that repository:

XmlConfigurator.ConfigureAndWatch(
                new FileInfo(
                    AppDomain.CurrentDomain.SetupInformation.ApplicationBase +
                    "log4net.config"));

Michael Schall

unread,
Jun 13, 2012, 11:07:14 AM6/13/12
to mybatis...@googlegroups.com
Looking at your config... I have never tried to setup ibatis logging
in the app.config...

The issue might be in...

<logFactoryAdapter
type="IBatisNet.Common.Logging.Impl.Log4NetLoggerFA,
IBatisNet.Common.Logging.Log4Net, Version=1.6.1.0, Culture=Neutral,
PublicKeyToken=ed781d9fc396c6ca">
<arg key="configType" value="file" />
<arg key="configFile" value="log4Net.config" />
</logFactoryAdapter>

Try using the following...

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

<iBATIS>
<logging>
<logFactoryAdapter
type="IBatisNet.Common.Logging.Impl.Log4NetLoggerFA,
IBatisNet.Common.Logging.Log4Net">
<arg key="configType" value="external" />
</logFactoryAdapter>
</logging>
</iBATIS>

This has worked great for us for years.

As for the layout of each statement, your layout is defined in your appender...

<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d - %m%n" />
</layout>

Looks like you want something more like...

<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %5level
%-30.30logger{2} %message%newline" />
</layout>

Mike
>> To: mybatis...@googlegroups.com

Andrea Tassinari

unread,
Jun 13, 2012, 11:40:21 AM6/13/12
to mybatis...@googlegroups.com
Hi,

this is my working configuration (running on IbatiNet.Common v1.6.2.x, log4net v1.2.10.0, Common.Logging v1.2.0, Common.Logging.Log4Net v1.2.0.2, IBatisNet.Commom.Logging.Log4Net v1.0.0.xxx)

please NOTE that
<arg key="configType" value="external"/> for both common and and iBATIS sections

///////////////APP.CONFIG

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

        <sectionGroup name="iBATIS">

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

    <common>
        <logging>
            <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net">
                <arg key="configType" value="EXTERNAL"/>
            </factoryAdapter>
        </logging>
    </common>

    <spring>
        <context>
            <resource uri="file://Resources/SpringNetMainObjects.xml"/>
        </context>
    </spring>

    <iBATIS>
        <logging>

            <logFactoryAdapter type="IBatisNet.Common.Logging.Impl.Log4NetLoggerFA, IBatisNet.Common.Logging.Log4Net">
                <arg key="configType" value="external"/>
            </logFactoryAdapter>
        </logging>
    </iBATIS>

///////////////

///example of log4net.config


        <appender name="IBatisLogFile" type="log4net.Appender.RollingFileAppender">
            <file value="Logs\iBatis.log" />
            <appendToFile value="true" />
            <datePattern value="yyyy-MM-dd" />
            <rollingStyle value="Date" />
            <staticLogFileName value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date{dd/MM/yyyy-HH:mm:ss} %m%newline%exception" />
            </layout>
        </appender>

        <appender name="IBatisMapperLogFile" type="log4net.Appender.RollingFileAppender">
            <file value="Logs\iBatisMapper.log" />
            <appendToFile value="true" />
            <datePattern value="yyyy-MM-dd" />
            <rollingStyle value="Date" />
            <staticLogFileName value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date{dd/MM/yyyy-HH:mm:ss} %m%newline%exception" />
            </layout>
        </appender>

        <appender name="IBatisCacheLogFile" type="log4net.Appender.RollingFileAppender">
            <file value="Logs\iBatisChache.log" />
            <appendToFile value="true" />
            <datePattern value="yyyy-MM-dd" />
            <rollingStyle value="Date" />
            <staticLogFileName value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date{dd/MM/yyyy-HH:mm:ss} %m%newline%exception" />
            </layout>
        </appender>


<logger name="IBatisNet">
            <level value="ALL"/>
            <appender-ref ref="IBatisLogFile" />
        </logger>

        <logger name="IBatisNet.DataMapper">
            <level value="ALL"/>
            <appender-ref ref="IBatisMapperLogFile" />
        </logger>

etcetera....

hope this help.

andrea

JKT

unread,
Jun 14, 2012, 5:23:15 AM6/14/12
to mybatis...@googlegroups.com
Hi Mike,

doing the change you have suggested file is been create with nothing inside....
>> To: mybatisnet-user@googlegroups.com

JKT

unread,
Jun 14, 2012, 5:28:14 AM6/14/12
to mybatis...@googlegroups.com, andrea.t...@gmail.com
Hi Andrea, I have updated the config files the you have suggest while running i am getting error
Unrecognized element <spring> in configuration file.

so, i have commented the <spring> tag and run the application on running debug\logs folder is created with files iBatis.log & iBatisMapper.log of 0kb with no data inside.

Is there is some logging code which need to be implemented in my class file (Program.cs) for logging the ibatis executed sql.

Thanks......

JKT

unread,
Jun 14, 2012, 11:28:07 PM6/14/12
to mybatis...@googlegroups.com, andrea.t...@gmail.com

Any update on

Is there is some logging code which need to be implemented in my class file (Program.cs) for logging the ibatis executed sql.




Ron Grabowski

unread,
Jun 15, 2012, 9:44:27 AM6/15/12
to mybatis...@googlegroups.com
Can you remove commons logging from the equation? I work on log4net and ibatis so you've reached the right person. Next steps would be to package up a small zip of your code somewhere so I can look through it.


From: JKT <jatin...@gmail.com>
To: mybatis...@googlegroups.com
Cc: andrea.t...@gmail.com
Sent: Thursday, June 14, 2012 11:28 PM
Subject: Re: IBatis vs Log4Net


Any update on
Is there is some logging code which need to be implemented in my class file (Program.cs) for logging the ibatis executed sql.




On Thursday, 14 June 2012 14:58:14 UTC+5:30, JKT wrote:
Hi Andrea, I have updated the config files the you have suggest while running i am getting error
Unrecognized element <spring> in configuration file.

so, i have commented the <spring> tag and run the application on running debug\logs folder is created with files iBatis.log & iBatisMapper.log of 0kb with no data inside.

Is there is some logging code which need to be implemented in my class file (Program.cs) for logging the ibatis executed sql.

Thanks......



On Wednesday, 13 June 2012 21:10:21 UTC+5:30, AndreaT wrote:
Hi,

this is my working configuration (running on IbatiNet.Common v1.6.2.x, log4net v1.2.10.0, Common.Logging v1.2.0, Common.Logging.Log4Net v1.2.0.2, IBatisNet.Commom.Logging. Log4Net v1.0.0.xxx)

please NOTE that
<arg key="configType" value="external"/> for both common and and iBATIS sections

///////////////APP.CONFIG

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

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

    <common>
        <logging>
            <factoryAdapter type="Common.Logging.Log4Net. Log4NetLoggerFactoryAdapter, Common.Logging.Log4net">

                <arg key="configType" value="EXTERNAL"/>
            </factoryAdapter>
        </logging>
    </common>

    <spring>
        <context>
            <resource uri="file://Resources/ SpringNetMainObjects.xml"/>
      <!--<section name="logging" type="IBatisNet.Common. Logging. ConfigurationSectionHandler, IBatisNet.Common" />-->
      <section name="logging" type="IBatisNet.Common. Logging. ConfigurationSectionHandler, IBatisNet.Common, Version=1.6.1.0, Culture=Neutral, PublicKeyToken= ed781d9fc396c6ca" />
    </sectionGroup>
    <!--<section name="log4net" type="log4net.Config. Log4NetConfigurationSectionHan dler, log4net" />-->

  </configSections>
  <iBATIS>
    <logging>
      <logFactoryAdapter type="IBatisNet.Common. Logging.Impl.Log4NetLoggerFA, IBatisNet.Common.Logging. Log4Net, Version=1.6.1.0, Culture=Neutral, PublicKeyToken= ed781d9fc396c6ca">
        <arg key="configType" value="file" />
        <arg key="configFile" value="log4Net.config" />
      </logFactoryAdapter>
    </logging>
  </iBATIS>
 
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4. 0"/>

  </startup>
</configuration>

log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net"
             type="log4net.Config. Log4NetConfigurationSectionHan dler, log4net, Version=1.2.10.0, Culture=Neutral, PublicKeyToken= bf100aa01a5c2784" />

  </configSections>
  <log4net debug="true">
    <appender name="RollingLogFileAppender"
              type="log4net.Appender. RollingFileAppender, log4net, Version=1.2.10.0, Culture=Neutral, PublicKeyToken= bf100aa01a5c2784" >
      <file value="ibat.txt" />
      <param name="MaxSizeRollBackups" value="2" />
      <param name="MaximumFileSize" value="1000KB" />
      <param name="RollingStyle" value="Size" />
      <param name="StaticLogFileName" value="true" />
      <appendToFile value="true" />
      <layout type="log4net.Layout. PatternLayout">
        <param name="ConversionPattern" value="%d - %m%n" />
      </layout>
    </appender>
    <!-- iBatis internal logging -->
    <logger name="IBatisNet.DataMapper">
      <level value="DEBUG" />
    </logger>
    <logger name="IBatisNet.DataMapper. Commands. DefaultPreparedCommand">
      <level value="DEBUG" />
    </logger>
    <logger name="IBatisNet.DataMapper. Configuration.Cache. CacheModel">

      <level value="DEBUG" />
    </logger>
    <logger name="IBatisNet.DataMapper. LazyLoadList">

      <level value="DEBUG" />
    </logger>
  </log4net>
</configuration>

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBatisNet.DataMapper;
using IBatisNet.Common;
using IBatisNet.Common.Logging;
using IBatisNet.Common.Logging.Impl;
using log4net;

namespace ConsoleApplication1
{
    public class Program
    {
        public static ISqlMapper myMapper
        {
            get
            {
                try
                {
                    ISqlMapper mapper = Mapper.Instance();
                    mapper.DataSource. ConnectionString = "Data Source=(local);Initial Catalog=testDb;Integrated Security=True";

JKT

unread,
Jun 20, 2012, 12:00:51 AM6/20/12
to mybatis...@googlegroups.com, Ron Grabowski
Hi Ron,

You can find my project at https://docs.google.com/open?id=0B3DNKFyEGv7td3F2QjgtMVlHVm8

Thanks,
Jatin


On Friday, 15 June 2012 19:14:27 UTC+5:30, Ron Grabowski wrote:
Can you remove commons logging from the equation? I work on log4net and ibatis so you've reached the right person. Next steps would be to package up a small zip of your code somewhere so I can look through it.

Ron Grabowski

unread,
Jun 25, 2012, 8:01:56 PM6/25/12
to mybatis...@googlegroups.com
ILSpy told me that IBatisNet.Common.Logging.Log4Net.dll:


has a reference to "log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821". The version of log4net.dll in the link you send me is "log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=bf100aa01a5c2784". Note the different public keys. I tweaked your code to use this app.config:

<?xml version="1.0"?>
<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>
  <iBATIS>
    <logging>
      <logFactoryAdapter type="IBatisNet.Common.Logging.Impl.Log4NetLoggerFA, IBatisNet.Common.Logging.Log4Net">
        <arg key="configType" value="external"/>
      </logFactoryAdapter>
    </logging>
  </iBATIS>
  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
      <layout type="log4net.Layout.PatternLayout" />
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="ConsoleAppender" />
    </root>
  </log4net>
</configuration>

and this Program.cs and I saw messages from both log objects:

public class Program
{
    private static readonly ILog ibatisLog = LogManager.GetLogger(typeof(Program));
    private static readonly log4net.ILog log4netLog = log4net.LogManager.GetLogger(typeof(Program));

    static void Main(string[] args)
    {
        XmlConfigurator.Configure();

        log4netLog.Debug("Hello World from log4net!");

        try
        {
            Mapper.InitMapper();
        }
        catch
        {
            // ignore
        }
        finally
        {
            if (ibatisLog.IsErrorEnabled)
            {
                ibatisLog.Error("Oh no!");    
            }
        }

        Console.ReadLine();
    }
}


From: JKT <jatin...@gmail.com>
To: mybatis...@googlegroups.com
Cc: Ron Grabowski <rongra...@yahoo.com>
Sent: Wednesday, June 20, 2012 12:00 AM

Subject: Re: IBatis vs Log4Net

JKT

unread,
Jun 25, 2012, 11:03:37 PM6/25/12
to mybatis...@googlegroups.com, Ron Grabowski

You are right log4net has different dll token keys.

Thanks Ron for the help the code works for me and my issue is resolved now.

I also want to thank Andrea who's resolution has help me alot to solve my log4net issue.


Thanks :) :)
Reply all
Reply to author
Forward
0 new messages