Log4net entry, log4net
Introduction
Almost all large applications have their own APIs for tracking and debugging. Once a program is deployed, it is unlikely that special debugging tools will be used. However, an administrator may need a powerful log system to diagnose and fix configuration problems.
Experience shows that logging is often an important part of the software development cycle. It has the following advantages: it provides a precise environment when the application is running, allowing developers to locate bugs in the application as soon as possible; once the Log output code is added to the program, logs can be generated and output during the program running without manual intervention. In addition, log information can be output to different places (such as the console and files) for future research.
Log4net is a log record package designed for this purpose and used in the. NET development environment.
Http://logging.apache.org/log4net/
Step 1: Install Log4net
Install-Package log4net
Step 2: Configure Log4net
log4net.config
Note: Set the log4net. config attribute"Copy to output directory"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 logs to the file --> <param name = "AppendToFile" value = "true"/> <! -- Log Retention days --> <param name = "MaxSizeRollBackups" value = "10"/> <! -- Whether the log file name is fixed --> <param name = "StaticLogFileName" value = "false"/> <! -- Log file name format: 2008-08-31.log --> <param name = "DatePattern" value = "yyyy-MM-dd & quot;. read. log & quot;"/> <! -- Rolling logs by Date --> <param name = "RollingStyle" value = "Date"/> <layout type = "log4net. layout. patternLayout "> <param name =" ConversionPattern "value =" % d [% t] %-5 p % c-% m % n % loggername "/> </layout> </appender> <! -- Log displayed on the console --> <appender name = "ColoredConsoleAppender" type = "log4net. appender. coloredConsoleAppender "> <mapping> <level value =" Fatal "/> <foreColor value =" Red, highIntensity "/> </mapping> <level value =" ERROR "/> <foreColor value =" Red, highIntensity "/> </mapping> <level value =" Warn "/> <foreColor value =" Yellow "/> </mapping> <layout type =" log4net. layout. patternLayout "> <conve RsionPattern value = "% n % date {HH: mm: ss, fff} [%-5 level] % m"/> </layout> <filter type = "log4net. filter. levelRangeFilter "> <param name =" LevelMin "value =" Info "/> <param name =" LevelMax "value =" Fatal "/> </filter> </appender> <root> <! -- (High) OFF> FATAL> ERROR> WARN> INFO> DEBUG> ALL (low) --> <level value = "all"/> <appender-ref = "ColoredConsoleAppender"/> <appender-ref = "RollingLogFileAppender"/> </root> </log4net> </configuration>
Step 3: Call
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. IO; using log4net; using log4net. config; namespace WHTR. demo. log4net {class Program {static void Main (string [] args) {InitLog4Net (); var logger = LogManager. getLogger (typeof (Program); logger. info ("message"); logger. warn ("warning"); logger. error ("exception"); logger. fatal ("error"); Console. readLine ();} private static void InitLog4Net () {var logCfg = new FileInfo (AppDomain. currentDomain. baseDirectory + "log4net. config "); XmlConfigurator. configureAndWatch (logCfg );}}}
A deep understanding of the Log4net Structure
Log4net has four main components:Logger (recorder),Repository),Appender (Attachment)AndLayout).
External Blog resources: http://www.cnblogs.com/dragon/archive/2005/03/24/124254.html