Detailed introduction and application of Log4net logging

  • 2020-06-03 06:39:52
  • OfStack

Log4net log

Summary:

Log4net is a tool that helps programmers output log information to various targets (consoles, files, databases, and so on). Log is the black box of the program, you can view the running process of the system through the log, so as to find the problems of the system. The role of the log: the steps of the running process, the success and failure of the record, record the key data, and then analyze the problem of the system. Because for the site, exception information cannot be displayed to the user, exception information can only be logged. When a website has a problem, a developer can look at the log and see what the problem is.

1. How to configure Log4net environment

1. Add "Application configuration File" (ES16en.config);

2. Add configuration to App. config or ES22en. 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 attribute "Copy to output directory" of log4ES34en. config to "Always copy";

5. Initialization

The program started to join log4net. Config. XmlConfigurator. Configure () to let the current Log4net work;

6. Write LonManager.GetLogger(typeof(Program)). .

Get the class ILog by passing the name of the logging class to be logged by LonManager.GetLogger (), so that you know in the log file which class the log was printed from, and then call the Debug method to print the message. Because there are more than one places within a class to print logs, ILog is generally declared as one static field.

7, Ilog. Error method output error message, the second parameter can pass the 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) 
    { 
      // use log4net Log.  
      log4net.Config.XmlConfigurator.Configure(); 
  
      ILog logWriter =log4net.LogManager.GetLogger("Test"); 
  
       logWriter.Info(" The message "); 
      logWriter.Warn(" warning "); 
      logWriter.Error(" abnormal "); 
      logWriter.Fatal(" error "); 
 } }} 
</pre> 
<pre></pre> 
<p>  </p> 
<h1><a name="t2"></a><strong><span style="font-family:SimHei; font-size:18px"> 3. Appender</span></strong></h1> 
<strong><span style="font-family:SimHei; font-size:18px">   Can be used in a configuration file Log4net the Appender Methods: To optimize; </span></strong> 
<pre></pre> 
<pre code_snippet_id="2146508" snippet_file_name="blog_20170123_2_6827063" name="code" class="csharp"><strong><span style="font-family:SimHei; font-size:18px"><img src="http://img.blog.csdn.net/20170123115415135?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvV0tYMTgzMzA2OTg1MzQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt=""> 
  For more, recommended reading:   
    <a target="_blank" href="http://www.cnblogs.com/jiajinyi/p/5884930.html">http://www.cnblogs.com/jiajinyi/p/5884930.html</a
   So that's me Log4net For a simple understanding of logging, please refer to the big bird passing by. </span></strong> 
<p></p></pre> 
<pre></pre> 

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: