代码改变世界

Log4net简单使用

2012-06-26 21:42  leo.wl2020  阅读(674)  评论(1编辑  收藏  举报

 

日志记录:

static log4net.ILog _log = log4net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
_log.Error(exception);

 

_log.InfoFormat("test:{0}", s.ToString())

 

Log4Net.config:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <!--日志记录组建配置-->
  <log4net>
    <!-- Console部分log输出格式的设定 -->
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
      </layout>
    </appender>

    <logger name="FileLogger">
      <level value="All" />
      <appender-ref ref="RollingLogFileAppender" />
    </logger>

    <!-- 日志文件部分log输出格式的设定 -->
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="Logs\Log_" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd'.txt'" />
      <staticLogFileName value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <header value="------------------------------------------------------------&#xD;&#xA;" />
        <ConversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
      </layout>
    </appender>

    <!-- Setup the root category, add the appenders and set the default level -->
    <root>
      <level value="ALL" />
      <appender-ref ref="ConsoleAppender" />
    </root>
  </log4net>
</configuration>

App.config:

<configSections>
    <section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

 

<log4net>
    <!-- Console部分log输出格式的设定 -->
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message%newline" />
      </layout>
    </appender>

    <!-- 日志文件部分log输出格式的设定 -->
    <appender name="RollingLogFileAppender"
      type="log4net.Appender.RollingFileAppender">
      <file value="Logs\Hosting_" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd'.txt'" />
      <staticLogFileName value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <!--<header value="[Header]&#xD;&#xA;" />
        <footer value="[Footer]&#xD;&#xA;" />-->
        <ConversionPattern value="�te - %message%newline" />
      </layout>
    </appender>

    <!-- Setup the root category, add the appenders and set the default level -->
    <root>
      <level value="ALL" />
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
  </log4net>

DLL:

log4net.dll