Original article, reprint must indicate source: http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/log4net%E4%B9%8B%E5%88%9D%E6%AD%A5%E4% ba%86%e8%a7%a3/
In the Daily project debugging, we often encounter some bug or abnormal error log, then write this bug or error log, can let us in the first time to find the location of the error, as well as the cause of the error, so that we correct the first time.
Often we will write the error log directly into the txt text, or you want to write the error log to the database, or you want to write a different error level in both the bug information or error log. With the above requirements then the use of the log4net described below is a relatively effective solution.
Let's describe how to log error logs to a document:
First, you have to prepare the log4net DLL file.
log4net Download Link: http://logging.apache.org/log4net/download_log4net.cgi
Friends using vs can download it directly in the NuGet Package manager of VS
Second, you need to configure the appropriate config file
Once the download is complete and the log4net DLL file is introduced into the project, you need to create a new config file (such as Log4net.config) in your project to populate the file with the following content.
1 <Configuration>2 <configsections>3 < Sectionname= "Log4net"type= "System.Configuration.IgnoreSectionHandler"/>4 </configsections>5 <log4net>6 <Root>7 < Levelvalue= "All"/>8 <Appender-refref= "Fileappender"/>9 </Root>Ten <!--operations that are stored to a file - One <Appendername= "Fileappender"type= "log4net." Appender.rollingfileappender "> A <paramname= "File"value= "Log\log.txt"/> - <Rollingstylevalue= "Date"/> - <paramname= "Appendtofile"value= "true"/> the <!--<param name= "Datepattern" value= "yyyy-mm-dd". txt ""/> - - <paramname= "Datepattern"value= "Log.txt"/> - <Layouttype= "log4net." Layout.patternlayout "> - <paramname= "Conversionpattern"value= "%date%n---threadid=[%t] level=%-5p message=%m%n"/> + </Layout> - </Appender> + </log4net> A </Configuration>
View Code
Locate the AssemblyInfo.cs file in your project and add the code to the last line of the file:
1 " Log4net.config " true)]
Or add code to the Application_Start () function of the project
1 log4net. Config.XmlConfigurator.Configure (new System.IO.FileInfo (Server.MapPath ("log4net.config ")));
This allows the log4net to be introduced into the project, as shown in the Code
1 ILog log =2 log. Error ("The variable does not exist ");
OK, this process goes down you can see the error statement in the Log.txt file under your project, the next one will explain in detail log4net to save the log to the document configuration.
A preliminary understanding of log4net