Brief introduction
Almost all large applications will have their own API for tracking debugging. Because once the program is deployed, it is less likely to take advantage of specialized debugging tools. However, an administrator may need a powerful logging system to diagnose and fix problems with the configuration.
Experience has shown that logging is often an important part of the software development cycle. It has several advantages: it provides an accurate environment for the application runtime and allows developers to find bugs in the application as quickly as possible, and once the log output code is added to the program, the log information can be generated and output without human intervention during the program's operation. In addition, the log information can be exported to different places (consoles, files, etc.) for future research purposes.
Log4net is designed for such a purpose, for. NET development environment for logging packages.
Official website: http://logging.apache.org/log4net/
How to use
First step: Installation of Log4net
Install-package log4net
Step two: Configuration of the Log4net
Log4net.config
Note: The Log4net.config property "copy to Output directory" is set to "Always copy"
<?xml version="1.0"encoding="Utf-8"?><configuration> <configSections> <section name="log4net"Type="log4net. Config.log4netconfigurationsectionhandler, Log4net"/> </configSections> <log4net> <appender name="Rollinglogfileappender"Type="log4net. Appender.rollingfileappender"> <!--log path--<param name="File"Value="D:\App_Log\ "/><!--whether to append the log to the file--<param name="Appendtofile"Value="true"/> <!--log retention days--<param name="maxsizerollbackups"Value="Ten"/> <!--The log file name is fixed--<param name="Staticlogfilename"Value="false"/> <!--log file name format is: -- ,- to.log--> <param name="Datepattern"Value="Yyyy-mm-dd". log""/> <!--logs by date-<param name="Rollingstyle"Value="Date"/> <layout type="log4net. Layout.patternlayout"> <param name="Conversionpattern"Value="%d [%t]%-5p%c-%m%n%loggername"/> </layout> </appender> <!--console display log--<appender name="Coloredconsoleappender"Type="log4net. Appender.coloredconsoleappender"> <mapping> <level value="ERROR"/> <forecolor value="Red, Highintensity"/> </mapping> <mapping> <level value="Info"/> <forecolor value="Green"/> </mapping> <layout type="log4net. Layout.patternlayout"> <conversionpattern value="%N%DATE{HH:MM:SS,FFF} [%-5level]%m"/> </layout> <filter type="log4net. Filter.levelrangefilter"> <param name="Levelmin"Value="Info"/> <param name="Levelmax"Value="Fatal"/> </filter> </appender> <root> <!--(h) OFF > FATAL > ERROR > WARN > I NFO > DEBUG > All (Low)-<level value=" All"/> <appender-ref ref="Coloredconsoleappender"/> <appender-ref ref="Rollinglogfileappender"/> </root> </log4net></configuration>
Step Three: Call
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.IO;usinglog4net;usinglog4net. Config;namespacelog4net{classProgram {Static voidMain (string[] args) {initlog4net (); varLogger = Logmanager.getlogger (typeof(program)); Logger. Info ("message"); Logger. Warn ("Warning"); Logger. Error ("Exception"); Logger. Fatal ("Error"); Console.ReadLine (); } Private Static voidinitlog4net () {varLogcfg =NewFileInfo (AppDomain.CurrentDomain.BaseDirectory +"Log4net.config"); Xmlconfigurator.configureandwatch (LOGCFG); } }}
Depth
Understanding the structure of log4net
Log4net has four main components, namely Logger (recorder), Repository (library), Appender (attachment), and layout.
External Blog Resources: http://www.cnblogs.com/dragon/archive/2005/03/24/124254.html
Log4net Configuration and use