首页 > 代码库 > log4net学习

log4net学习

Log4net 主要类:

Appender   配置附加器有很多种,主要用到的几种:

log4net.Appender.AdoNetAppender <记录到数据库配置access,sqlserver或者其他的数据库>  

log4net.Appender.FileAppender <记录到记录日志到单个文件>  

log4net.Appender.EventLogAppender <记录到记录日志到操作系统的事件>  

log4net.Appender.RollingFileAppender <记录到记录日志到文件,可以设置文件的名字,或者多个文件>

<appender name="ADONetAppender_SqlServer" type="log4net.Appender.AdoNetAppender"> <appender name="AdoNetAppender_Access" type="log4net.Appender.AdoNetAppender"> <appender name="FileAppender" type="log4net.Appender.FileAppender"> <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender"> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">

  

具体应用方法:  

第一步:把log4net加入到引用  

第二步:配置log4net config 可以配置成数据库或者文件或者操作系统事件里面  

第三步:

       如果是网站:

          在global.asax里面新增配置

 

         void Application_Start(object sender, EventArgs e)         {                  // 在应用程序启动时运行的代码                  log4net.Config.XmlConfigurator.Configure();         }

  

      如果是web项目和winform项目或者控制台项目:

      要在AssemblyInfo.cs 新增

 [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)] 

或者:

[assembly: log4net.Config.XmlConfigurator()]  (把log4net的配置配置在web的配置文件时)

      具体常用的是 log4net.Appender.RollingFileAppender   可以根据日期生成日志文件   和log4net.Appender.FileAppender区别是后者只能生成一个文件,   log4net.Appender.FileAppender写的日志或追加或覆盖操作一个文件。

      还有配置写日志时:

<root>       <level value="ALL"/>       <appender-ref ref="ADONetAppender_SqlServer"/>  </root>  <logger name="ADONetAppender_SqlServer">       <level value="All"/>       <appender-ref ref="ADONetAppender_SqlServer"/>       <appender-ref ref="FileAppender"></appender-ref>  </logger>

用后者需要在 在log4net.LogManager.GetLogger 是填写正确的logger,不然获得不到节点就写不了日志

  
log4net.ILog log = log4net.LogManager.GetLogger("ADONetAppender_SqlServer");
log.Info("hello world!");
而前者用起来更方便,可以写出在那个类报的错
log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log.Error("error", new Exception("hello world! 报错了!"));

常用配置如下:

     <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">       <!--日志的路径-->       <file value="log\log" />       <!--是否覆盖,默认是追加true-->       <appendToFile value="true"/>       <!--文件名称-->       <DatePattern value="yyyyMMdd &quot;.txt&quot;"></DatePattern>       <!--设置无限备份=-1 ,最大备份数为1000-->       <maxSizeRollBackups value="1000"/>       <!--每个文件的最大20k-->       <maximumFileSize value="20"/>       <!--名称是否可以更改为false为可以更改-->       <param name="StaticLogFileName" value="false" />             <param name="RollingStyle" value="Composite" />       <layout type="log4net.Layout.PatternLayout">         <param name="ConversionPattern" value="%d [%r] [%t] %-5p %c  - %m%n%n" />       </layout> </appender>

写到sqlserver的配置:

<appender name="ADONetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">       <!--<bufferSize value="http://www.mamicode.com/10" />-->       <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>       <connectionString value="Server=localhost;UID=sa;PWD=sa;DataBase=log;Max Pool Size=1000"/>       <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="http://www.mamicode.com/%date{yyyy‘-‘MM‘-‘dd HH‘:‘mm‘:‘ss‘.‘fff}" />                 </layout>                 -->         <layout type="log4net.Layout.RawTimeStampLayout"/>       </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> 

写到access配置:

<appender name="AdoNetAppender_Access" type="log4net.Appender.AdoNetAppender">       <bufferSize value="1" />       <connectionString value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\工作目录\\代码\\Demo\\LogDemo\\db\\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" />         </layout>       </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>     <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">       <mapping>         <level value="ALL" />         <foreColor value="White" />         <backColor value="Red, HighIntensity" />       </mapping>       <layout type="log4net.Layout.PatternLayout">         <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />       </layout>     </appender>     <!--写入到文件,是累加,不生成新的文件-->     <appender name="FileAppender" type="log4net.Appender.FileAppender">       <!--日志写入路径-->       <file value="log\log-file.txt" />       <!--overwritten/appended to default =true 默认是追加-->       <appendToFile value="true" />       <layout type="log4net.Layout.PatternLayout">         <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />       </layout>     </appender>

<!--写入到操作系统-->

    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">       <ApplicationName value="msg"></ApplicationName>       <LogName value="测试"></LogName>       <MachineName value="zhangqi"></MachineName>       <layout type="log4net.Layout.PatternLayout">         <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>       </layout>     </appender> 

<!--写入到文件-->

     <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">       <!--<file value="http://www.mamicode.com/log/log.txt"/>-->       <!--<file name="Client.Logging" value="http://www.mamicode.com/log/log" />-->       <file value="log\log" />       <!--是否覆盖,默认是追加true-->       <appendToFile value="true"/>       <DatePattern value="yyyyMMdd &quot;.txt&quot;"></DatePattern>       <!--设置无限备份=-1 ,最大备份数为1000-->       <maxSizeRollBackups value="1000"/>       <!--每个文件的最大20k-->       <maximumFileSize value="20"/>       <!--名称是否可以更改为false为可以更改-->       <param name="StaticLogFileName" value="false" />       <!--文件名称-->       <!--<param name="DatePattern" value="http://www.mamicode.com/yyyyMMdd".txt"" />-->       <param name="RollingStyle" value="Composite" />       <!--<param name="RollingStyle" value="http://www.mamicode.com/Composite" />-->       <layout type="log4net.Layout.PatternLayout">         <param name="ConversionPattern" value="%d [%r] [%t] %-5p %c  - %m%n%n" />       </layout>     </appender>

<!--写入到文件,是累加,不生成新的文件-->

    <appender name="FileAppender" type="log4net.Appender.FileAppender">       <!--日志写入路径-->       <file value="log\log-file.txt" />       <!--overwritten/appended to default =true 默认是追加-->       <appendToFile value="true" />       <layout type="log4net.Layout.PatternLayout">         <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />       </layout>     </appender>
 <root>       <level value="ALL"/>       <appender-ref ref="ADONetAppender_SqlServer"/>       <appender-ref ref="RollingLogFileAppender"/>       <appender-ref ref="EventLogAppender"></appender-ref>       <appender-ref ref="AdoNetAppender_Access"></appender-ref>       <appender-ref ref="ColoredConsoleAppender"></appender-ref>       <appender-ref ref="FileAppender"></appender-ref>     </root>   <logger name="ConsoleApp.LoggingExample">       --><!--<appender-ref ref="B" />--><!--       <level value="http://www.mamicode.com/All"/>       <appender-ref ref="ADONetAppender_SqlServer"/>       <appender-ref ref="RollingLogFileAppender"/>     </logger>     <logger name="LoggingExample">       --><!--<appender-ref ref="B" />--><!--       <level value="http://www.mamicode.com/All"/>       <appender-ref ref="ADONetAppender_SqlServer"/>       <appender-ref ref="FileAppender"></appender-ref>     </logger>

 

log4net学习