Log4net-powerful Log System

Source: Internet
Author: User
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 ):

 

    1. Using system;
    2. Using system. Collections. Generic;
    3. Using system. LINQ;
    4. Using system. Web;
    5. Using system. Web. UI;
    6. Using system. Web. UI. webcontrols;

 

    1. Namespace webapplication1
    2. {
    3. Public partial class webform1: system. Web. UI. Page
    4. {
    5. Protected void page_load (Object sender, eventargs E)
    6. {
    7. Log4net. config. xmlconfigurator. Configure ();
    8. }
    9. Private Static log4net. ilog logger = log4net. logmanager. getlogger (typeof (webform1 ));
    10. Protected void button#click (Object sender, eventargs E)
    11. {
    12. Log4net. logmanager. getlogger (typeof (webform1). debug (" ");
    13. Logger. debug ("hao ");
    14. Logger. Error ("cuowu1le1", new exception (); // an exception object
    15. }
    16. }
    17. }

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:

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.