一、安装NuGet包
二、添加配置
builder. Logging. AddLog4Net ( "CfgFile/log4net.config" ) ;
三、配置log4net.config文件
< ? xml version= "1.0" encoding= "utf-8" ? >
< log4net>
< ! -- Define some output appenders -- >
< appender name= "rollingAppender" type= "log4net.Appender.RollingFileAppender" >
< ! -- value = "logs/log.log" -- >
< file value = "logs/" / >
< ! -- 追加日志内容-- >
< appendToFile value = "true" / >
< ! -- 防止多线程时不能写Log, 官方说线程非安全-- >
< lockingModel type= "log4net.Appender.FileAppender+MinimalLock" / >
< ! -- 可以为: Once| Size| Date| Composite-- >
< ! -- Composite为Size和Date的组合-- >
< rollingStyle value = "Composite" / >
< ! -- 当备份文件时, 为文件名加的后缀-- >
< datePattern value = "yyyyMMdd/"log.log"" / >
< ! -- 日志最大个数, 都是最新的-- >
< ! -- rollingStyle节点为Size时, 只能有value 个日志-- >
< ! -- rollingStyle节点为Composite时, 每天有value 个日志-- >
< maxSizeRollBackups value = "20" / >
< ! -- 可用的单位: KB| MB| GB-- >
< maximumFileSize value = "3MB" / >
< ! -- 置为true , 当前最新日志文件名永远为file节中的名字-- >
< staticLogFileName value = "false" / >
< ! -- 输出级别在INFO和ERROR之间的日志-- >
< filter type= "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>
< ! -- error日志 -- >
< appender name= "errorAppender" type= "log4net.Appender.RollingFileAppender" >
< file value = "logs/" / >
< appendToFile value = "true" / >
< lockingModel type= "log4net.Appender.FileAppender+MinimalLock" / >
< rollingStyle value = "Composite" / >
< datePattern value = "yyyyMMdd/"error.log"" / >
< maxSizeRollBackups value = "20" / >
< maximumFileSize value = "3MB" / >
< staticLogFileName value = "false" / >
< filter type= "log4net.Filter.LevelRangeFilter" >
< param name= "LevelMin" value = "ERROR" / >
< param name= "LevelMax" value = "ERROR" / >
< / filter>
< layout type= "log4net.Layout.PatternLayout" >
< conversionPattern value = "%date [%thread] %-5level %logger - %message%newline" / >
< / layout>
< / appender>
< root>
< ! -- 控制级别,由低到高: ALL| DEBUG| INFO| WARN| ERROR| FATAL| OFF-- >
< ! -- OFF: 0 -- >
< ! -- FATAL: FATAL-- >
< ! -- ERROR: ERROR, FATAL-- >
< ! -- WARN: WARN, ERROR, FATAL-- >
< ! -- INFO: INFO, WARN, ERROR, FATAL-- >
< ! -- DEBUG: INFO, WARN, ERROR, FATAL-- >
< ! -- ALL: DEBUG, INFO, WARN, ERROR, FATAL-- >
< priority value = "ALL" / >
< level value = "INFO" / >
< ! -- 使用上面配置的那个规则-- >
< appender- ref ref = "rollingAppender" / >
< appender- ref ref = "errorAppender" / >
< / root>
< / log4net>
四、记录日志
private readonly ILogger< WeatherForecastController> _logger;
public WeatherForecastController ( ILogger< WeatherForecastController> logger)
{
_logger = logger;
_logger. LogInformation ( $" { this . GetType ( ) } ,info,接口访问Info" ) ;
_logger. LogDebug ( $" { this . GetType ( ) } ,debugger,接口访问Debugger" ) ;
_logger. LogWarning ( $" { this . GetType ( ) } ,warning,接口访Warning" ) ;
_logger. LogError ( $" { this . GetType ( ) } ,error,接口访问Error" ) ;
}
五、查看日志文件