Home  >  Article  >  Java  >  Detailed introduction and application of Log4net logging

Detailed introduction and application of Log4net logging

巴扎黑
巴扎黑Original
2017-08-22 16:40:302165browse

Log4net Log9TBHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

Summary: 9TBHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

Log4net is a tool that helps programmers output log information to various targets (console, file, database, etc.). The log is the black box of the program. You can use the log to view the running process of the system and discover system problems. The function of logs: record the steps, successes and failures of the running process, record key data, and then analyze system problems. Because for websites, exception information cannot be displayed to users, and exception information can only be recorded in the log. When there is a problem with the website, the developer knows the problem by looking at the logs. 9TBHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
9TBHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

##1. How to configure the Log4net environment9TBHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

1. Add "Application Configuration File" (App.config);

9TBHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

2. Add configuration in App.config or Web.config:

9TBHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
9TBHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

  <log4net>   <!-- Define some output appenders -->   <appendernameappendername="RollingLogFileAppender"type="log4net.Appender.RollingFileAppender">    <file value="test.txt"/>    <appendToFilevalueappendToFilevalue="true"/>    <maxSizeRollBackupsvaluemaxSizeRollBackupsvalue="10"/>    <maximumFileSizevaluemaximumFileSizevalue="1024KB"/>    <rollingStylevaluerollingStylevalue="Size"/>    <staticLogFileNamevaluestaticLogFileNamevalue="true"/>    <layouttypelayouttype="log4net.Layout.PatternLayout">     <conversionPattern value="%date[%thread] %-5level %logger - %message%newline"/>    </layout>   </appender>   <root>    <level value="DEBUG"/>    <appender-refrefappender-refref="RollingLogFileAppender"/>   </root>  </log4net>
3. Add Log4net.dll reference;

9TBHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

4. The attribute "Copy to output directory" of log4net.config is set to "Always copy";

9TBHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

5. Initialization:

9TBHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network
9TBHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Add log4net.Config.XmlConfigurator.Configure() at the beginning of the program to let the current Log4net It works;

9TBHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
9TBHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

6. In the place where you want to print the log Write LonManager.GetLogger(typeof(Program)).Debug("information");.

9TBHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network
9TBHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

Pass the log to be recorded through LonManager.GetLogger() Get the class ILog from the class name, so that in the log file you can know which class outputs the log, and then call the Debug method to output the message. Because logs need to be printed in more than one place within a class, ILog is generally declared as a static field.

9TBHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network
9TBHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

7. Use the Ilog.Error method to output error information. An Exception object can be passed as the second parameter. Log.Error("****Error"+ex);

9TBHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
9TBHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

2. Log4NetDemo9TBHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
9TBHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

<html>
 <head></head>
 <body>
  <pre code_snippet_id="2146508" snippet_file_name="blog_20170123_2_6827063" name="code" class="csharp">
namespace Log4NetDemo {   
class Program   {    
 static void Main(string[] args)     {       //使用log4net记录日志。    
   log4net.Config.XmlConfigurator.Configure();      
      ILog logWriter =log4net.LogManager.GetLogger(&quot;Test&quot;);       
         logWriter.Info(&quot;消息&quot;);       logWriter.Warn(&quot;警告&quot;);     
           logWriter.Error(&quot;异常&quot;);       logWriter.Fatal(&quot;错误&quot;); 
            } 
            }
            } 
            
  

三、Appender

可以在配置文件中使用Log4net的Appender方法进行优化;
 
  
  <strong><span style="font-family:SimHei; font-size:18px">
  <img src="http://www.php.cn/20170123115415135?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvV0tYMTgzMzA2OTg1MzQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /> 
   更多内容,推荐阅读:  
    </span></strong>

9TBHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

The above is the detailed content of Detailed introduction and application of Log4net logging. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn