I. Introduction:
Almost all large applications have their own APIs for tracking and debugging. Because onceProgramAfter deployment, it is unlikely that special debugging tools will be used. However, an administrator may need a powerful log system to diagnose and fix configuration problems.
Experience shows that logging is often an important part of the software development cycle. It has the following advantages: it provides a precise environment when the application is running, allowing developers to locate bugs in the application as soon as possible; once the log output is added to the programCodeThe program can generate and output log information while running the program without manual intervention. In addition, log information can be output to different places (such as the console and files) for future research.
Log4net is a log record package designed for this purpose and used in the. NET development environment.
Ii. Usage:
1) Import log4net. dll,
2) the configuration file is the configuration. config configuration file for the website,
3) <? XML version = "1.0" encoding = "UTF-8"?>
4)
5) <! --
6) for more information about how to configure ASP. NET applications, visit
7) http://go.microsoft.com/fwlink? Linkid = 169433
8) -->
9)
10) <configuration>
11) <configsections>
12) <section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net"/>
13) </configsections>
14) <system. Web>
15) <compilation DEBUG = "true" targetframework = "4.0"/>
16) </system. Web>
17) <log4net>
18) <! -- Define some output appenders -->
19) <appender name = "rollinglogfileappender" type = "log4net. appender. rollingfileappender">
20) <file value = "mylog. log"/>
21) <appendtofile value = "true"/>
22) <maxsizerollbackups value = "10"/>
23) <maximumfilesize value = "1024kb"/>
24) <rollingstyle value = "size"/>
25) <staticlogfilename value = "true"/>
26) <layout type = "log4net. layout. patternlayout">
27) <conversionpattern value = "% date [% thread] %-5 level % logger-% message % newline"/>
28) </layout>
29) </appender>
30) <root>
31) <level value = "debug"/>
32) <appender-ref = "rollinglogfileappender"/>
33) </root>
34) </log4net>
35) </configuration>
36) Note: <file value = "mylog. log"/> indicates that the record file name is mylog. log.
37) The test code is as follows (this is the background code of a website with only one button ):
- Using system;
- Using system. Collections. Generic;
- Using system. LINQ;
- Using system. Web;
- Using system. Web. UI;
- Using system. Web. UI. webcontrols;
- Namespace webapplication1
- {
- Public partial class webform1: system. Web. UI. Page
- {
- Protected void page_load (Object sender, eventargs E)
- {
- Log4net. config. xmlconfigurator. Configure ();
- }
- Private Static log4net. ilog logger = log4net. logmanager. getlogger (typeof (webform1 ));
- Protected void button#click (Object sender, eventargs E)
- {
- Log4net. logmanager. getlogger (typeof (webform1). debug (" ");
- Logger. debug ("hao ");
- Logger. Error ("cuowu1le1", new exception (); // an exception object
- }
- }
- }
What you are not familiar with is the key code. Run the command first and click.
Check the website root directory:
Open this is the text we tested: