Log4net entry (console) and log4net entry Console
Log4net is the log4j of Apache.. NET version for help.. NET developers output log information to various output sources (Appender). Common output sources include the console, log files, and databases. This article mainly discusses how to output log files to the console, log files, and SQL Server databases in the console application.
Log4net is easy to use. It takes only five steps to complete. The following uses the console application as an example to describe how to use log4net to output logs to the console.
First, we will demonstrate the simplest example to output the log information to the console. The steps are as follows:
1. Create a console application named Log4netConsoleApplication, and then use the NuGet Package Manager to install log4net. The latest stable version is 2.0.5, after the installation is complete, the log4net assembly is displayed under the reference node of the project, indicating that the installation is successful.
2. Expand the Properties node in the Log4netConsoleApplication application, double-click the AssemblyInfo. cs file, and add the following code at the end of the file:
1 [assembly: log4net. Config. XmlConfigurator (ConfigFile = "Log4net. config", Watch = true)]
3. Add the application configuration file and name it Log4net. config (note that the file name is consistent with the value of ConfigFile in the previous step), view the properties of the file, and set the value of "Copy to output directory" to "always copy ".
4. Edit the Log4net. config file, as shown in the following figure:
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <configuration> 3 <configSections> 4 <section name = "log4net" type = "log4net. config. log4NetConfigurationSectionHandler, log4net "/> 5 </configSections> 6 7 <log4net> 8 <! -- Output logs to the console --> 9 <appender name = "leleappender" type = "log4net. appender. consoleAppender "> 10 <layout type =" log4net. layout. patternLayout "> 11 <conversionPattern value =" % date [% thread] %-5 level % logger-% message % newline "/> 12 </layout> 13 </appender> 14 15 <root> 16 <! -- Control level, from low to high: ALL | DEBUG | INFO | WARN | ERROR | FATAL | OFF --> 17 <! -- For example, if the definition level is INFO, the level of INFO is down. For example, the DEBUG log will not be recorded --> 18 <! -- If no level value is defined, the default value is DEBUG --> 19 <level value = "ALL"/> 20 <! -- Output logs to the console --> 21 <appender-ref = "leleappender"/> 22 </root> 23 </log4net> 24 </configuration>
5. Double-click the project's Program. cs file to ensure that the code in the file is as follows:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Log4netConsoleApplication 8 { 9 class Program10 {11 private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);12 static void Main(string[] args)13 {14 log.Debug("debug");15 log.Info("info");16 log.Warn("warn");17 log.Error("error");18 log.Fatal("fatal");19 20 Console.ReadKey();21 }22 }23 }
After the preceding five steps are completed, run the console application and you can see that the log information is output in the console window, as shown in:
Now, how to output the log information to the console is complete.