There is a log4net User Guide in the garden, which is detailed. We recommend that you use log4net.
If you don't want to spend too much time on research, you can do it in five minutes by following the steps below:
1. Add a reference to log4net. dll
2. modify the configuration file. Take app. config as an example. The reference content is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Configuration> <configsections> <section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net "/> </configsections> <log4net> <logger name =" jimmy. logger "> <! -- All-> debug-> Info-> warn-> error-> fatal-> off levels are added in sequence, that is, if info is set, logs of the debug type are not recorded. If it is set to error, logs with only two levels of error/fatal will be recorded --> <level value = "Warn"/> <appender-ref = "rollinglogfileappender"/> </logger> <appender name = "rollinglogfileappender" type = "log4net. appender. rollingfileappender "> <! -- Whether to append to a file --> <Param name = "appendtofile" value = "true"/> <! -- The maximum log file size is 10 MB --> <Param name = "maxsizerollbackups" value = "10"/> <Param name = "staticlogfilename" value = "false"/> <! -- Log file name prefix --> <Param name = "file" value = "log \"/> <! -- Log file names are generated by date --> <Param name = "rollingstyle" value = "date"/> <! -- Log file name generation format: yyyymmdd.txt --> <Param name = "datepattern" value = "yyyymmdd".txt & quot;"/> <! -- Log record format --> <layout type = "log4net. layout. patternlayout "> <Param name =" conversionpattern "value =" % d [% T] %-5 p % C [% x]-% m % N "/> </Layout> </appender> </log4net> </configuration>
3. Example Code
Using system; using log4net; namespace console_demo {class program {static void main (string [] ARGs) {log4net. config. xmlconfigurator. configure (); ilog log = logmanager. getlogger ("jimmy. logger "); // The name here, which corresponds to the console in the logger name in config. writeline ("log4net started... "); console. writeline ("isinfoenabled = {0} \ nisdebugenabled = {1} \ niswarnenabled = {2} \ niserrorenabled = {3} \ nisfatalenabled = {4}", log. isinfoenabled, log. isdebugenabled, log. iswarnenabled, log. iserrorenabled, log. isfatalenabled); If (log. isinfoenabled) {log. info ("info test");} If (log. isdebugenabled) {log. debug ("Debug test");} If (log. iswarnenabled) {log. warn ("Warn test");} If (log. iserrorenabled) {log. error ("error test");} If (log. isfatalenabled) {log. fatal ("Fatal test");} console. readkey ();}}}After running Program Under the same level directory, a log file named 201102.16.txt is automatically created and generated. The content is similar to the following:
20:41:25, 347 [6948] warn Jimmy. Logger [(null)]-Warn Test
20:41:25, 463 [6948] Error Jimmy. Logger [(null)]-Error Test
20:41:25, 465 [6948] fatal Jimmy. Logger [(null)]-fatal Test Note: Because the log4net node of config is configured with a log level of warn, the debug information lower than the warn level is not recorded in the log. Example Source code Download: http://files.cnblogs.com/yjmyzz/log4net_demo.7z