How to use log4net in C # environment

Source: Internet
Author: User
Tags configuration settings log4net
1. Download Log4net Development Package

2. Create a new Windows application and add Log4net.dll references to the project

3. Add the application configuration file to the project and name it Log4net.config, as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<!--Register A handler for the log4net section-->
<configSections>
<section name= "log4net" type= "log4net". Config.log4netconfigurationsectionhandler,log4net "/>
</configSections>

<!--This section contains the Log4net configuration settings-->
<log4net>
<!--Define some output appenders-->
<appender name= "Rollinglogfileappender" type= "log4net". Appender.rollingfileappender ">
<param name= "File" value= "Log\\log-iqslive.txt"/>
<param name= "Appendtofile" value= "true"/>
<param name= "Datepattern" value= "Yyyy-mm-dd"/>
<param name= "Rollingstyle" value= "Date"/>
<param name= "maxsizerollbackups" value= "/>"
<layout type= "log4net. Layout.patternlayout ">
<param name= "Conversionpattern" value= "%d{yyy-mm-dd HH:mm:ss} [%t] [%c]-[%-5p]%m%n"/>
</layout>
</appender>
<root>
<level value= "Debug"/>
<appender-ref ref= "Rollinglogfileappender"/>
</root>
</log4net>s
</configuration>

4. Add the following statement to the AssemblyInfo.cs file of the project:

[Assembly:log4net. Config.xmlconfigurator (Watch = true, ConfigFile = "Log4net.config")]

5. Add in the From1 class:

private static readonly log4net. ILog log = log4net. Logmanager.getlogger (System.Reflection.MethodBase.GetCurrentMethod (). DeclaringType);

6. Add a button and add the following code to the response event:

Log. Info ("button is clicked!");

7. Copy the Log4net.config file to the debug directory and run the program click button to see the log file under the Logs folder in the Debug directory

Related Article

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.