Common.Logging log4net Common.Logging.Log4Net 配置

1、log4net 单独配置

  log4net支持多种格式的日志输出,我这里只配置输出到本地的txt文件这种格式。

  <log4net>
    <root>
      <appender-ref ref="RollingFileAppender" />
    </root>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="..\\Data\\Log\\Log.txt" />
      <param name="AppendToFile" value="true" />
      <param name="MaxSizeRollBackups" value="10" />
      <param name="MaximumFileSize" value="5MB" />
      <param name="RollingStyle" value="Size" />
      <param name="StaticLogFileName" value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%n[时间]:%d%n[级别]:%p%n[内容]:%m%n" />
      </layout>
    </appender>
  </log4net>

2、Common.Logging 配置

<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="INFO"/>
            <arg key="dateTimeFormat" value="HH:mm:ss:fff"/>
        </factoryAdapter>
        </logging>
    </common>

3、Common.Logging+log4net+ Common.Logging.Log4Net配置

  在联合使用Common.Logging和log4net日志组件时需要注意组件之间的版本是否匹配。

  参考:http://stackoverflow.com/questions/8172623/how-to-configure-logging-in-spring-net-using-log4net

  组件下载:https://github.com/net-commons/common-logging/tree/2.3.0.RC1

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

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

  <log4net>
    <root>
      <appender-ref ref="RollingFileAppender" />
    </root>
    
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
      </layout>
    </appender>
    
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="..\\Data\\Log\\Log.txt" />
      <param name="AppendToFile" value="true" />
      <param name="MaxSizeRollBackups" value="10" />
      <param name="MaximumFileSize" value="5MB" />
      <param name="RollingStyle" value="Size" />
      <param name="StaticLogFileName" value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%n[时间]:%d%n[级别]:%p%n[内容]:%m%n" />
      </layout>
    </appender>
    
  </log4net>

 

  4、输出效果截图

 

 

posted @ 2014-11-28 19:45  晓风之季  阅读(2252)  评论(0编辑  收藏  举报