Preface : Log4net is a relatively simple and practical logging tool, referencing DLLs, setting up configuration files, instantiating objects, understanding the components and principles, you can get started.
Learning reason : Like me this kind of solo repair is different from your family's disciples, there are so many resources and mentors. At first I was not in the call and understand the log this thing, my own program is also directly F11 step-by-step debugging. But when the program was published to the server, I found that because the program running environment changed, in the local good, in the server is not. I can't always install a vs on the server and then step through the debugging.
Exploration history:
1. Write a log to a text in the Application_Error () method in global (problem: Lock others to wait).
2. The error is stored in the collection, the Application_Error () method reads the collection, and then the text is saved. (Problem: Not enough to install force)
3. Error in queue, open a thread, responsible for removing from the queue, and then depositing text. (explained later in the blog)
4.log4net (easy to get started, information can be output to the console, text)
5. Use Redis and log4net to complete distributed logging (explained later in the blog)
Simple Introduction
log4net (Log for Net, also log for Java) is an Apache open source application of the. Net Framework's logging tools, the essence of which is the potential of the thousand-edged, moving fingers between
Logger: Log Record object
Appender: Logging Target
Repository: Library
Layout: Layouts
Declare a configuration file to configure Log4net (can be configured in WEB.CONFG, but it will look cumbersome)
<log4net>
<root>
<level value= "DEBUG"/>
<appender-ref ref= "Logfileappender"/>
<appender-ref ref= "Consoleappender"/>
</root>
<logger name= "Testapp.logging" >
<level value= "DEBUG"/>
</logger>
<appender name= "Logfileappender" type= "log4net. Appender.fileappender ">
<param name= "File" value= "C://log-file.txt"/>
<param name= "Appendtofile" 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 [%x]-%m%n"/>
</layout>
<filter type= "log4net. Filter.levelrangefilter ">
<param name= "Levelmin" value= "DEBUG"/>
<param name= "Levelmax" value= "WARN"/>
</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>
In Properties.cs, point to this configuration file [Assembly:log4net. Config.domconfigurator (configfile= "Config/log4net.config", Watch=true)]
Appender in the configuration is to specify the output of this log, C://log.txt run after the C-disk will also be a file Log.txt folder
When used:: private static ILog log = Logmanager.getlogger (typeof (current class name));
Log. Debug (String. FORMT ("exception: {0}", "I actively throw an exception")
We call GetLogger () as a class (type) parameter,
The type of the passed class (class) can be obtained using the typeof (Classname) method
The typeof above is used to get the System.Type object of the type. Gets the class object for the specified class: This allows additional information to be obtained through the class object. If the class is not an abstract class, which assembly is located, etc.
Of course, there are a lot of usage and configuration, please refer to tutorial video:
Baidu Network Disk : Link: http://pan.baidu.com/s/1c1EyiL6 Password: Bjug
Reference Documentation:
Http://www.cnblogs.com/Raymond201508/p/4934322.html
http://blog.csdn.net/daichenghua/article/details/4806688
Thanks for the above authors.
Log4net from shallow to more shallow