log4net .NET Core Edition uses, log4net 2.0.7 release there for some time, version 2.0.6 from the beginning has been to support .NET Core.
There are introduced NLog .NET Core version before use, ASP.NET Core development -Logging use NLog write log files .
ASP.NET Core has built-in logging support, you can easily output to the console. Use log4net to write to the log file and console output.
.NET Core project uses
Create a new .NET Core project, select Console Application .
Add a reference:
Install-Package log4net
Using a simple configuration, the log output to the console before with .NET version slightly different, you need to specify Repository.
public static void the Main ( String [] args) { ILoggerRepository Repository = LogManager.CreateRepository ( " NETCoreRepository " ); // default a simple configuration, to the console BasicConfigurator.configure (Repository); the ILog log = LogManager.GetLogger (repository.Name , " NETCorelog4net " ); log.info ( " NETCorelog4net log " ); log.info ( " Test log " ); log.error ( "error"); log.Info("linezero"); Console.ReadKey(); }
Run the program appears as follows:
The following configuration increases, let the output to a file.
Add in the project a profile, add a log4net.config here, reads as follows:
<? xml Version = " 1.0 " encoding = " UTF-8 " ?> <log4net> <-! the Define some the appenders in the Output -> <appender name = " rollingAppender " of the type = " log4net.Appender.RollingFileAppender " > <File = value " log \ log.txt " /> <-! additional log contents -> <appendToFile value = " to true " /> <-! prevent multiple threads can not write log, the official said non-thread-safe -> <lockingModel of the type = "log4net.Appender.FileAppender+MinimalLock"/> <- can be:! Once | Size | Date | Composite -> ! <- Composite is a combination of Size and Date -> <rollingStyle value = " Composite " /> <- When the backup file! for the file name plus the suffix -> <datePattern value = " YYYYMMDD.txt " /> <! - the maximum number of logs, are the latest -> <! - when rollingStyle node as Size, only value log -> ! <- rollingStyle node as Composite, there is value a daily log -> <maxSizeRollBackups value = " 20 " /> <- units available:! KB | MB | GB -> < value = maximumFileSize " 3MB " /> <! - set to true, the most current log file name is always the name of the section file -> <staticLogFileName value = " to true " /> <! - output level between INFO and ERROR logs -> < type = filter " log4net.Filter.LevelRangeFilter " > <param name = " LevelMin " value = " ALL " /> <param name = " LevelMax " value = " FATAL " /> </ filter> <layout type = " log4net. Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/> </layout> </appender> <root> <priority value="ALL"/> <level value="ALL"/> <appender-ref ref="rollingAppender" /> </root> </log4net>
It can be defined as follows LoggerHelper
public class LoggerHelper { private static ILog log; static LoggerHelper() { if (log == null) { ILoggerRepository repository = LogManager.CreateRepository("NETCoreRepository"); XmlConfigurator.Configure(repository, new FileInfo("log4net.config")); log = LogManager.GetLogger(repository.Name, "NETCorelog4net"); } } /// <summary> /// 普通日志 /// </summary> /// <param name="message"></param> /// <param name="exception"></param> public static void Info(string message, Exception exception = null) { if (exception == null) log.Info(message); else log.Info(message, exception); } /// <summary> /// 告警日志 /// </summary> /// <param name="message"></param> /// <param name="exception"></param> public static void Warn(string message, Exception exception = null) { if (exception == null) log.Warn(message); else log.Warn(message, exception); } /// <summary> /// 错误日志 /// </summary> /// <param name="message"></param> /// <param name="exception"></cannot be null.param> public static void Error(string message, Exception exception = null) { if (exception == null) log.Error(message); else log.Error(message, exception); } }
log4net .NET Core Edition uses, log4net 2.0.7 release there for some time, version 2.0.6 from the beginning has been to support .NET Core.
There are introduced NLog .NET Core version before use, ASP.NET Core development -Logging use NLog write log files .
ASP.NET Core has built-in logging support, you can easily output to the console. Use log4net to write to the log file and console output.
.NET Core project uses
Create a new .NET Core project, select Console Application .
Add a reference:
Install-Package log4net
Using a simple configuration, the log output to the console before with .NET version slightly different, you need to specify Repository.
public static void the Main ( String [] args) { ILoggerRepository Repository = LogManager.CreateRepository ( " NETCoreRepository " ); // default a simple configuration, to the console BasicConfigurator.configure (Repository); the ILog log = LogManager.GetLogger (repository.Name , " NETCorelog4net " ); log.info ( " NETCorelog4net log " ); log.info ( " Test log " ); log.error ( "error"); log.Info("linezero"); Console.ReadKey(); }
Run the program appears as follows:
The following configuration increases, let the output to a file.
Add in the project a profile, add a log4net.config here, reads as follows:
<? xml Version = " 1.0 " encoding = " UTF-8 " ?> <log4net> <-! the Define some the appenders in the Output -> <appender name = " rollingAppender " of the type = " log4net.Appender.RollingFileAppender " > <File = value " log \ log.txt " /> <-! additional log contents -> <appendToFile value = " to true " /> <-! prevent multiple threads can not write log, the official said non-thread-safe -> <lockingModel of the type = "log4net.Appender.FileAppender+MinimalLock"/> <- can be:! Once | Size | Date | Composite -> ! <- Composite is a combination of Size and Date -> <rollingStyle value = " Composite " /> <- When the backup file! for the file name plus the suffix -> <datePattern value = " YYYYMMDD.txt " /> <! - the maximum number of logs, are the latest -> <! - when rollingStyle node as Size, only value log -> ! <- rollingStyle node as Composite, there is value a daily log -> <maxSizeRollBackups value = " 20 " /> <- units available:! KB | MB | GB -> < value = maximumFileSize " 3MB " /> <! - set to true, the most current log file name is always the name of the section file -> <staticLogFileName value = " to true " /> <! - output level between INFO and ERROR logs -> < type = filter " log4net.Filter.LevelRangeFilter " > <param name = " LevelMin " value = " ALL " /> <param name = " LevelMax " value = " FATAL " /> </ filter> <layout type = " log4net. Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/> </layout> </appender> <root> <priority value="ALL"/> <level value="ALL"/> <appender-ref ref="rollingAppender" /> </root> </log4net>
It can be defined as follows LoggerHelper
public class LoggerHelper { private static ILog log; static LoggerHelper() { if (log == null) { ILoggerRepository repository = LogManager.CreateRepository("NETCoreRepository"); XmlConfigurator.Configure(repository, new FileInfo("log4net.config")); log = LogManager.GetLogger(repository.Name, "NETCorelog4net"); } } /// <summary> /// 普通日志 /// </summary> /// <param name="message"></param> /// <param name="exception"></param> public static void Info(string message, Exception exception = null) { if (exception == null) log.Info(message); else log.Info(message, exception); } /// <summary> /// 告警日志 /// </summary> /// <param name="message"></param> /// <param name="exception"></param> public static void Warn(string message, Exception exception = null) { if (exception == null) log.Warn(message); else log.Warn(message, exception); } /// <summary> /// 错误日志 /// </summary> /// <param name="message"></param> /// <param name="exception"></cannot be null.param> public static void Error(string message, Exception exception = null) { if (exception == null) log.Error(message); else log.Error(message, exception); } }