How to use Log4net in a. Net WinForm Project

Source: Internet
Author: User
Tags bool config header zip log4net

Almost all large applications will have their own APIs for tracking debugging. Because once a program is deployed, it is less likely to use specialized debugging tools. However, an administrator may need a strong logging system to diagnose and fix configuration problems. Log4net is a very famous open source component of log logs, and the basic framework stems from another well-known sister component-log4j in the Java domain. Log4net logging is very powerful. It can be divided into different levels of the log, than the different styles, the log output to different media.

An example is given to illustrate the use of log4net:

1, download the Log4net component:

Http://logging.apache.org/log4net/download.html

Http://archive.apache.org/dist/incubator/log4net/1.2.10/incubating-log4net-1.2.10.zip

2, extract the Log4net component DLL:

Incubating-log4net-1.2.10.zip\log4net-1.2.10\bin\net\2.0\release:

Log4net.xml

Log4net.dll

3, copy the Log4net.dll to the. NET project directory under: Bin/debug;

4, add the Log4net.dll to the project reference by right-clicking the reference under the VS Tool solution project and selecting Log4net.dll OK;

5, under the Bin/debug to establish log4net configuration file Log4net.xml, the file contents are as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <configuration> <configSections> <secti On Name= "log4net" type= "log4net. Config.log4netconfigurationsectionhandler, log4net-net-1.0 "/> </configSections> <log4net&
Gt <!--log level: The level allowed method Boolean attribute priority off highest FATAL void FATAL (...);
BOOL isfatalenabled; rror void Error (...);
BOOL iserrorenabled; WARN void WARN (...);
BOOL iswarnenabled; info void info (...);
BOOL isinfoenabled; debug void Debug (...);
BOOL isdebugenabled; All Lowest--> <root> <level value= "WARN"/> & 
    
        Lt;appender-ref ref= "Logfileappender"/> <appender-ref ref= "Consoleappender"/> </root> <logger name= "Com.suntom" > <level value= "DEBUG"/> </logger> <!--log Output format:%m (message): Output log messages, such as Ilog.debug (...) Output of a message%n (new line): Wrap%d(datetime): Output the moment that the current statement is run%r (run time): The number of milliseconds that the output program consumes from running to the current statement%t (thread ID): The current statement is in the Threads ID%p (priority): The current priority level of the log, that is, debug, INFO, WARN ...
Equal to%c (class): The name of the current log object, for example: The pattern string is:%-10c-%m%n code: ILog log=logmanager.getlogger ("Exam.Log"); Log.
    Debug ("Hello"); The output is in the following form: Exam.log-hello%l: The line number where the output statement is located%F: The file name%-number that contains the output statement: The minimum length of the item, or, if not enough, a space to fill--> <appender name= "Logfileappender" type= "log4net. Appender.fileappender "> <param name=" File "value="/log.txt "/> <param" name= The File "value=" true "/> <layout type=" log4net. Layout.patternlayout "> <param name= Header" value= "[header]\r\n"/> <param name = "Footer" value= [footer]\r\n "/> <param name=" Conversionpattern "value="%d [%t]%-5p%c-%m%n "/&gt
            ; </layout> <filter type= "log4net.
   Filter.levelrangefilter "> <param name=" levelmin "value=" DEBUG "/>             <param name= "Levelmax" value= "FATAL"/> </filter> </appender> <appender name= "Consoleappender" type= "log4net". Appender.consoleappender "> <layout type=" log4net.
            Layout.patternlayout "> <param name=" Conversionpattern "value="%d [%t]%-5p%c [%x]-%m%n "/> </layout> </appender> </log4net> </configuration>

6, configure log4net in the project, configure the method: AssemblyInfo.cs (Web project for web.config), add the following:

[Assembly:log4net. Config.xmlconfigurator (configfile= "./log4net.xml", configfileextension= "xml", Watch=true)]

7, use log4net in the project, such as class: Com.suntom.testClass:

Log4net. ILog log = log4net. Logmanager.getlogger (this. GetType ());

Log. Debug ("Enter Initmenus ()");

8, start the program and run to the log output, after exiting the program can be seen under the Bin/debug log file:

Log.txt:

[header]\r\n2011-07-04 22:38:58,226 [] DEBUG com.suntom.testclass-enter Initmenus ()

[Footer]

9, here, the log4net can be used.

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.