Home >Java >javaTutorial >A detailed introduction to Log4net logging and a detailed introduction to the application code

A detailed introduction to Log4net logging and a detailed introduction to the application code

黄舟
黄舟Original
2017-03-08 10:26:541770browse

This article mainly introduces the detailed introduction of Log4net logging and related application information. Friends who need it can refer to

Log4net log

Summary :

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.

1. How to configure the Log4net environment

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

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

  <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;

4. Set the property "Copy to output directory" of log4net.config to "Always copy";

5. Initialization:

Add log4net.Config.XmlConfigurator.Configure at the beginning of the program () Let the current Log4net work;

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

Pass the name of the log class to be recorded through LonManager.GetLogger() to obtain the class ILog, so that in the log file you will 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.

7. Use the Ilog.Error method to output error information. The second parameter can pass an Exception object. Log.Error("****Error"+ex);

2. Log4NetDemo

<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("Test"); 
  
       logWriter.Info("消息"); 
      logWriter.Warn("警告"); 
      logWriter.Error("异常"); 
      logWriter.Fatal("错误"); 
 } }} 
 

三、Appender

可以在配置文件中使用Log4net的Appender方法进行优化;
 
<strong><span style="font-family:SimHei; font-size:18px">
<img src=" 
I0JBQkFCMA==/dissolve/70/gravity/Center" alt=""> 
 更多内容,推荐阅读:  
    <a target="_blank" href="http://www.cnblogs.com/jiajinyi/p/5884930.html">http://www.php.cn/;/a>  
   以上是我对Log4net日志记录的简单理解,请路过的大鸟指点。</span></strong>  
<p></p>


The above is the detailed content of A detailed introduction to Log4net logging and a detailed introduction to the application code. 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