Use log4net in winform

Source: Internet
Author: User

First download log4net (nonsense ...)
1. Add log4net reference
2. Create a configuration file under the winform program. The default configuration file is app. config. The changes are as follows:
3-20 rows are newly added

Configuration File
1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <configuration>
3 <configsections>
4 <section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net"/>
5 </configsections>
6
7 <log4net>
8 <root>
9 <level value = "all"/>
10 <appender-ref = "logfileappender"/>
11 </root>
12
13 <appender name = "logfileappender" type = "log4net. appender. fileappender">
14 <Param name = "file" value = "log-file.txt"/>
15 <Param name = "appendtofile" value = "true"/>
16 <layout type = "log4net. layout. patternlayout">
17 <Param name = "conversionpattern" value = "% d [% T] %-5 p % C [% x] & lt; % x {auth} & gt; % N-% m % N "/>
18 </layout>
19 </appender>
20 </log4net>
21 </configuration>
22
23

3. After adding, add the following to assemblyinfo. CS:

[Assembly: log4net. config. domconfigurator (configfileextension = "Config", watch = true)]

4. Then add logs in the program using the following method (I wrote them directly in the main function for testing): 1 static void main ()
2 {
3 log4net. ilog log = log4net. logmanager. getlogger ("test ");
4 log. debug ("test ");
5}
6

5. When the producer runs, the log file log-file.txt is generated under the DEBUG directory of the target project. The content is as follows: 1 14:02:38, 890 [11] Debug test [(null)] <(null)>
2-Test

In this way, you can use log4net to record logs in the program. Of course, this is just a helloworld-level example that allows log4net to run. For specific usage instructions, see the official log4net documentation. There are also some Chinese introduction, such as http://www.cnblogs.com/dragon/archive/2005/03/24/124254.html (my essay is to refer to this article and the author's reply to write a little)
Enjoy log4net!

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.