Mybatis for .net 的日志输出

目前mybatis for .net 可以支持对外的日志输出包括SQL语句和参数等信息,用于调试,目前支持的输出方式有三种

具体的mybatis的引用和使用方式不再本文的讨论范围

1.命令窗口的输出

命令窗口的输出比较乱,因为限于窗口的大小,SQL比较长的时候显示就比较乱了,具体的实现方式,是添加config文件,app.config或者web.config

第一步:添加应用程序配置文件

第二步:在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.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>

最后的app.config或者web.config 中完整配置如下

<?xml version="1.0" encoding="utf-8" ?>
<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.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>

 

第三步:开始运行测试吧,在运行时候会出现命令窗口显示SQL和参数,效果如下 怎么样比较乱吧

 

2.使用log4net的格式输出到文本文件中

既然是使用log4net的方式肯定要引用mybatis中自带的IBatisNet.Common.Logging.Log4Net.dll,只需要引用就可以了,不需要在代码上特别写什么。

剩下的就是配置了 。因为使用log4net的格式输出日志。就牵涉到log4net的日志格式配置保存在那里的问题,由此引出了第三种方式,

和mybatis的节点一起写在引用程序保存在app.config或者web.config中是第二中方式。log4net 独立保存在另一个配置文件中是第三种方式。

具体的配置如下,保存在app.config或者web.config中

在configuration中添加如下配置:

  <!--日志输出到外部log 使用内置log4net节点的配置-->
  <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>

上面一部分mybatis的配置,下面的log4Net节点是log4Net的配置,可以自己写日子的格式

最后的app.config或者web.config 中完整配置如下

<?xml version="1.0" encoding="utf-8" ?>
<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>
 
  <!--日志输出到外部log 使用内置log4net节点的配置-->
  <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>

 

第三种方式 单独把log4Net的配置保存log4Net.config中( 推荐)

该方式和第二种差不多,只是把log4Net 的配置独立保存在另一个配置文件中了,在配置比较多的时候,又想对日志的格式有更细致的控制时,推荐采用该方式

具体的配置如下:

第一步: app.config或者web.config中的配置如下 

<?xml version="1.0" encoding="utf-8" ?>
<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>

  <!--日志输出到外部log 使用外部Log4Net.config 需要删除log4.net节点的内容-->
  <iBATIS>
    <logging>
      <logFactoryAdapter type="IBatisNet.Common.Logging.Impl.Log4NetLoggerFA, IBatisNet.Common.Logging.Log4Net">
        <arg key="configType" value="file-watch" />
        <arg key="configFile" value="Log4Net.config"/> <!--Value表示log4Net.conig的路径,必须正确-->
      </logFactoryAdapter>
    </logging>
  </iBATIS>
</configuration>

 第二步: log4Net.config 的配置如下,不想写出日志时可以把level的值设为 “ off ",配置是从其他的大神那拿来的,想要在深入了解的话可以看

http://blog.csdn.net/pfe_nova/article/details/12225349

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net"
             type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>
  <!--站点日志配置部分-->
  <log4net>
    <root>
      <!--控制级别,由低到高: ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF-->
      <!--比如定义级别为INFO,则INFO级别向下的级别,比如DEBUG日志将不会被记录-->
      <!--如果没有定义LEVEL的值,则缺省为DEBUG-->
      <level value="DEBUG"/>
      <appender-ref ref="RollingFileAppender"/>
    </root>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <!--日志文件名开头 日志的位置-->
      <file value="c:\Log\TestLog4net.TXT"/>
      <!--多线程时采用最小锁定-->
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <!--日期的格式,每天换一个文件记录,如不设置则永远只记录一天的日志,需设置-->
      <datePattern value="(yyyyMMdd)"/>
      <!--是否追加到文件,默认为true,通常无需设置-->
      <appendToFile value="true"/>
      <!--变换的形式为日期,这种情况下每天只有一个日志-->
      <!--此时MaxSizeRollBackups和maximumFileSize的节点设置没有意义-->
      <!--<rollingStyle value="Date"/>-->
      <!--变换的形式为日志大小-->
      <!--这种情况下MaxSizeRollBackups和maximumFileSize的节点设置才有意义-->
      <RollingStyle value="Size"/>
      <!--每天记录的日志文件个数,与maximumFileSize配合使用-->
      <MaxSizeRollBackups value="10"/>
      <!--每个日志文件的最大大小-->
      <!--可用的单位:KB|MB|GB-->
      <!--不要使用小数,否则会一直写入当前日志-->
      <maximumFileSize value="2MB"/>
      <!--日志格式-->
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%t]%-5p %c - %m%n"/>
      </layout>
    </appender>
  </log4net>
</configuration>

最后在目标位置会产生日志文件,快去尝试吧

 

最后有个问题mybatis应该可以在代码里面里面设置日志的级别,目前还没找到,只能通过log4Net 的level来决定是否输出日志,不太合理

有知道的,求告知

源码:https://files.cnblogs.com/Dunk/MyBatis.zip

 

posted on 2014-05-07 21:40  Dunk  阅读(1355)  评论(0编辑  收藏  举报

导航