Log4 basic configuration

Preface: As a programmer you have to learn to debug, for a debug both cases the problem can not be found, you have to learn to look at the log, the log must learn to see what will happen to your school to write the log, then teach you to configure C # Log4

 

The first step, you configure the corresponding parameters in the configuration file

<!--配置log4-->
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
  <log4net>
    <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
    <!-- Set root logger level to ERROR and its appenders -->
    <root>
      <level value="ALL" />
      <appender-ref ref="SysAppender" />
    </root>
    <!-- Print only messages of level DEBUG or above in the packages -->
    <logger name="WebLogger">
      <level value="DEBUG" />
    </logger>
    <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net">
      <param name="File" value="logs/" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <param name="DatePattern" value="&quot;log4net&quot;/&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />
      <param name="StaticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>
    <appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net">
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>
  </log4net>

The second step you need to configure the program in a position to start or you need

// Configure log4 
 XmlConfigurator.Configure ( new new the FileInfo (the Server.MapPath ( " ~ / the Web.config " ))); 
// The following are examples

 

The third step; to start recording

= log log4net.LogManager.GetLogger log4net.ILog ( the this .GetType ());
 // the information written to the log file runtime 
 log.info ( " Run time information is written to the log file " )
 // write to the log file error message 
 log.error ( " error writing to the log file information " )
 // write debugging information to the log file 
(log.debug " write debugging information to the log file " )

Step Four: You can choose to view the information you recorded in your log files

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/loushengjie/p/log4.html