I tried to configure and write the log by myself because I saw that the company project was useful to write the log. Don't say much nonsense, see below for instructions on configuring and using Log4net:
1. Download Log4net.dll, the address is: http://logging.apache.org/log4net/personal suggestions to download the source version, if you can not write the log debugging.
2 introduce the DLL into the project.
3 There are two ways to configure log4net: use Web. config or create a new **.config configuration file. Below I was configured with a new configuration file, configuration files and descriptions are as follows
<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<system.web>
<compilation debug= "True" targetframework= "4.0"/>
</system.web>
<configSections>
<section name= "log4net" type= "log4net. Config.log4netconfigurationsectionhandler,log4net "/>
</configSections>
<log4net debug= "true" >
<root>
<appender-ref ref= "Logfileappenderbydate"/>
</root>
<!--<logger name= "MyLogger" >
<level value= "DEBUG"/>
--><!--corresponds to the name of the Appender you configured, you can configure multiple logger and multiple Appender counterparts, and root and Looger nodes must have a name value of Appender that corresponds to the response--- <!--
<appender-ref ref= "Logfileappenderbydate"/>
</logger>-->
<!--split log files by date One rollingfileappender means you can write multiple files---
<appender name= "Logfileappenderbydate" type= "log4net. Appender.rollingfileappender ">
<!--whether to resume-
<param name= "Appendtofile" value= "true"/>
<!--minimum lock model to allow multiple processes to write to the same file--
<param name= "Lockingmodel" value= "log4net. Appender.FileAppender.MinimalLock "/>
<!--save path--
<param name= "File" value= "D:\log\"/>
<!--file name format-
<param name= "Datepattern" value= "YyyyMMdd"/>
<!--Staticlogfilename is true, new files cannot be created dynamically, and the file configuration node specifies the log file name--
<param name= "Staticlogfilename" value= "false"/>
<!--How to create a new file by date-
<param name= "Rollingstyle" value= "Date"/>
<layout type= "log4net. Layout.patternlayout ">
<param name= "Conversionpattern" value= "time:%d%n level:%level%n class Name:%c%n file:%F the%l line%n log contents:%m% n-----------------------------------------%n%n "/>
</layout>
</appender>
</log4net>
</configuration>
4. After the configuration, to let the program find your configuration file, so in the properties of Assembly.cs add the following code:
Specify log4net configuration file configfile is the absolute path to your log4net profile
[Assembly:log4net. Config.xmlconfigurator (configfile = @ "D:\Test\TestJST\log4net.config", Watch = True)]
5. Use:
If you define only root and do not define a Looger node, use this method to get the logger
ILog log = Logmanager.getlogger (System.Reflection.MethodBase.GetCurrentMethod (). DeclaringType);
Looger node, you can use this (MyLogger corresponds to the name value of the logger node you configured):
ILog log = Logmanager.getlogger ("MyLogger");
Then write the log on the line:
Log. Debug ("log Message");
Last Log effect:
Date: 2014-08-28 13:43:52,161
Level: DEBUG
Class Name: MyLogger
File: D:\Test\TestJST\CallWebService.aspx.cs line 32nd
Log content: Try dividing by 0.
-----------------------------------------
PS: The beginning has been written unsuccessful log, the loss in the logger node <appender-ref ref= "Logfileappenderbydate"/> configuration, the ref to correspond to <appender name= " Logfileappenderbydate "Type=" log4net. Appender.rollingfileappender ">. Otherwise, when debugging the source will say can not find the corresponding appender, hey, their carelessness.
Simple configuration and use of log4net