Log4net Entry Example

Source: Internet
Author: User

I have read a few tutorials on the Internet and have some minor problems. After some tests, I tried it and wrote the simplest one that can be used. I hope it will be useful to beginners!

First, add the following in APP. config:

 

Code

  <  Configsections  >  
< Section Name = "Log4net"
Type = "Log4net. config. log4netconfigurationsectionhandler, log4net-net-1.0" />
</ Configsections >
< Log4net >
< Root >
< Level Value = "Warn" />
< Appender-ref Ref = "Logfileappender" />
< Appender-ref Ref = "Leleappender" />
</ Root >
< Appender Name = "Logfileappender" Type = "Log4net. appender. fileappender" >
< File Value = "Log-file.txt" />
< Appendtofile Value = "True" />
< Layout Type = "Log4net. layout. patternlayout" >
< Header Value = "[Header] & amp; #13; & amp; #10 ;" />
< Footer Value = "[Footer] & amp; #13; & amp; #10 ;" />
< Conversionpattern Value = "% Date --- [% thread] %-5 level '% logger': % message % newline" />
</ Layout >
</ Appender >
< Appender Name = "Leleappender" Type = "Log4net. appender. leleappender" >
< Layout Type = "Log4net. layout. patternlayout" >
< Param Name = "Conversionpattern"
Value = "% D [% T] %-5 p (% C)-% m % N"
/>
</ Layout >
</ Appender >
</ Log4net >

Then add the following line in assemblyinfo. CS.

[Assembly: log4net. config. xmlconfigurator ()]

FinallyCode.

Code

  Public     Class  Workerthreadexample
{
Private Static Readonly Log4net. ilog = Log4net. logmanager. getlogger ( " Test " );

Static Void Main ()
{
If (Log. isinfoenabled) log. Fatal ( " Application [consoleapp] Start " );
Log. Info ( " Error " );
Log. Fatal ( " Wadsadasd " );

}

}

 

For more information, see the document andArticleHowever, the config in this article seems to have some problems, mainly because the param settings are inconsistent with those in this document.

http://dragon.cnblogs.com/archive/2005/03/24/124254.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.