Log4net 1.2.10.0 的配置使用

 
项目中添加引用log4net.dll,目前最新版是1.2.10.0。
 
初始化时执行一次Configure。
log4net.Config.XmlConfigurator .Configure(new FileInfo("logconfig.xml"));
 
在需要使用的类中声明log。使用 typeof(类名) 做参数。
private log4net.ILog log = log4net.LogManager.GetLogger(typeof(SomeClass));
 
使用:
if(log.IsDebugEnabled)
  log.Debug("some message");
//...
try{...}
catch(Exception ex)
{
  log.Error("some error", ex);
}
============================== ==
logconfig.xml的内容如下:(主体是官方示例中的配置,居然有拼写和格式错误)
 
<?xml version="1.0" encoding="utf-8" ?>
 <!-- This section contains the log4net configuration settings -->
 <log4net>
  <!-- Define some output appenders -->
  <appender name="RollingLogFileAppender" type=" log4net.Appender.RollingFileApp ender">
   <file value="rolling-log.txt" />
   <appendToFile value="true" />
   <maxSizeRollBackups value="10" />
   <maximumFileSize value="10MB" />
   <rollingStyle value="Size" />
   <staticLogFileName value="true" />
   <layout type="log4net.Layout.PatternLayout">
    <header value="[Header]&#13;&#10;" />
    <footer value="[Footer]&#13;&#10;" />
    <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
   </layout>
  </appender>
  <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
   <file value="log-file.txt" />
   <!-- Example using environment variables in params -->
   <!-- <file value="${TMP}/log-file.txt" /> -->
   <appendToFile value="true" />
   <!-- An alternate output encoding can be specified -->
   <!-- <encoding value="unicodeFFFE" /> -->
   <layout type="log4net.Layout.PatternLayout">
    <header value="[Header]&#13;&#10;" />
    <footer value="[Footer]&#13;&#10;" />
    <conversionPattern value="%date [%thread] %-5level %logger [%ndc] &lt;%property{auth}&gt; - %message%newline" />
   </layout>
   <!-- Alternate layout using XML   
   <layout type="log4net.Layout.XMLLayout" /> -->
  </appender>
  <appender name="ConsoleAppender" type=" log4net.Appender.ConsoleAppende r">
   <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%ndc] &lt;%property{auth}&gt; - %message%newline" />
   </layout>
  </appender>
  <!--
  <appender name="NetSendAppender" type="log4net.Appender.NetSendAppende r">
   <threshold value="ERROR" />
   <server value="SQUARE" />
   <recipient value="nicko" />
   <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
   </layout>
  </appender>
  -->
  <!-- Example of how to configure the AdoNetAppender  
  <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender" >   
   <connectionString value="Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/Work/cvs_root /log4net-1.2/access.mdb;UID=;PWD=;" />
   <layout type=" log4net.Layout.PatternLayout">    
    <conversionPattern value="INSERT INTO Log ([Date],[Thread],[Level], [Logger],[Message]) VALUES ('%date','%thread','%level',' %logger','%message')" />
   </layout>
  </appender>
  -->
  <!--
  <appender name="A" type="log4net.Appender.ForwardingAppe nder" >
   <threshold value="WARN"/>   
   <appender-ref ref="ConsoleAppender" />  
  </appender>
  <appender name="B" type="log4net.Appender.ForwardingAppe nder" >
   <filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="DEBUG"/>
    <levelMax value="INFO"/>
   </filter>
   <appender-ref ref="ConsoleAppender" />
  </appender>
  -->
  <!-- Example of how to configure the AdoNetAppender to connect to MS Access -->
  <!--
  <appender name="ADONetAppender_Access" type="log4net.Appender.AdoNetAppender">
   <connectionString value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Work/cvs_root /log4net- 1.2/access.mdb;User Id=;Password=;" />
   <commandText value="INSERT INTO Log ([Date],[Thread],[Level], [Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" />
   <parameter>
    <parameterName value="@log_date" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%date" />
    </param>
   </parameter>
   <parameter>
    <parameterName value="@thread" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout ">
     <conversionPattern value="%thread" />
    </layout>
   </parameter>
   <parameter>
    <parameterName value="@log_level" />
    <dbType value="String" />
    <size value="50" />
    <layout type="log4net.Layout.PatternLayout ">
     <conversionPattern value="%level" />
    </layout>
   </parameter>
   <parameter>
    <parameterName value="@logger" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%logger" />
    </layout>
   </parameter>
   <parameter>
    <parameterName value="@message" />
    <dbType value="String" />
    <size value="1024" />
    <layout type="log4net.Layout.PatternLayout ">
     <conversionPattern value="%message" />
    </layout>
   </parameter>
  </appender>
  -->
  <!-- Example of how to configure the AdoNetAppender to connect to MS SQL Server -->
  <!--
  <appender name="ADONetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">
   <bufferSize value="1" />
   <connectionType value="System.Data.SqlClient.SqlConnec tion , System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 " />
   <connectionString value="data source=SQLSVR;initial catalog=test_log4net;integrated security=false;persist security info=True;User ID=sa;Password=sa" />
   <commandText value="INSERT INTO Log ([Date],[Thread],[Level], [Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" />
   <parameter>
    <parameterName value="@log_date" />
    <dbType value="DateTime" />
    -->
    <!--
    <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
    </layout>
    
    <layout type="log4net.Layout.RawTimeStampLayo ut" />
   </parameter>
   <parameter>
    <parameterName value="@thread" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%thread" />
    </layout>
   </parameter>
   <parameter>
    <parameterName value="@log_level" />
    <dbType value="String" />
    <size value="50" />
    <layout type="log4net.Layout.PatternLayout ">
     <conversionPattern value="%level" />
    </layout>
   </parameter>
   <parameter>
    <parameterName value="@logger" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%logger" />
    </layout>
   </parameter>
   <parameter>
    <parameterName value="@message" />
    <dbType value="String" />
    <size value="4000" />
    <layout type="log4net.Layout.PatternLayout ">
     <conversionPattern value="%message" />
    </layout>
   </parameter>
  </appender>
  -->
  <!-- Setup the root category, add the appenders and set the default level -->
  <root>
   <level value="ALL" />
   <appender-ref ref="LogFileAppender" />
   <appender-ref ref="ConsoleAppender" />
   <appender-ref ref="RollingLogFileAppender" />
   <!-- <appender-ref ref="ADONetAppender_SqlServer" /> -->
   <!--<appender-ref ref="NetSendAppender" />-->
   <!-- <appender-ref ref="A" /> -->
  </root>
  <!-- Specify the level for some specific categories -->
  <!--
  <logger name="Test.SimpleClass">
   <level value="ERROR" />
   <appender-ref ref="RollingLogFileAppender" />
  </logger>
 
  -->
 </log4net>

 

你可能感兴趣的:(Log4net 1.2.10.0 的配置使用)