Log4net Console, winform,webapplication use

Source: Internet
Author: User

One, log4net console, winform,webapplication use

1. First use NuGet to add log4net to the console project

log4j the specific meaning of each symbol:%d%5p%c{1}:%l-%m%n
Log4j.properties

# %m The message specified in the output code
#
#
#
#
#
#
# %l where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code.
log4j.appender.stdout.layout.conversionpattern=%d [%t]%-5p%c-%m%n

#日志文件 Daily
Log4j.appender.a1=org.apache.log4j.dailyrollingfileappender
Log4j.appender.a1.file=f:/mylog.txt
Log4j.appender.a1.layout=org.apache.log4j.patternlayout
#% time [% thread]%log level% full class name-% message% line break
log4j.appender.a1.layout.conversionpattern=%d [%t]%-5p%c-%m%n

2. Add the following in the console project's app. Config (as long as the add type, path, etc. of the settings log)

<log4net>
<appender name= "Rollinglogfileappender" type= "log4net. Appender.rollingfileappender ">
<param name= "File" value= "Log\log.txt"/>
<param name= "Appendtofile" value= "true"/>
<param name= "maxsizerollbackups" value= "/>"
<param name= "maximumFileSize" value= "2MB"/>
<param name= "Rollingstyle" value= "Size"/>
<param name= "Staticlogfilename" value= "true"/>
<layout type= "log4net. Layout.patternlayout ">
<param name= "Conversionpattern" value= "%-15p%d [%c]%m%n"/>
</layout>
</appender>
<root>
<level value= "All"/>
<appender-ref ref= "Rollinglogfileappender"/>
</root>
</log4net>

3. Add the following to the AssemblyInfo.cs in the properties of the console project

[Assembly:assemblyfileversion ("1.0.0.0")]
[Assembly:log4net. Config.xmlconfigurator (configfileextension = "config", Watch = True)]

4. Use in console project startup item projects

Static log4net. ILog Log = log4net. Logmanager.getlogger (typeof (program));
static void Main (string[] args)
{
Log.info ("111111111111111111111111");
Log.warn ("111111111111111111111111");
Log.error ("111111111111111111111111");
}

Log4net Console, winform,webapplication use

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.