1. 安装两个nuget包:
2. 创建log4net.config 配置文件,并选中log4net文件点击鼠标右键选择属性将"复制到输出目录"改为"始终复制"
<?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>
3. 在 Program.cs 文件中添加配置
//log4net builder.Logging.AddLog4Net("CfgFile/log4net.config");
4. 使用
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace WEBAPI.Controllers { [Route("api/[controller]/[action]")] [ApiController] public class LoginController : ControllerBase { private readonly ILogger<LoginController> _logger; /// <summary> /// 构造函数 /// </summary> /// <param name="logger"></param> public LoginController(ILogger<LoginController> 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"); } [HttpPost] public string Login() { return "sssssssss"; } } }
5. 查看生成的日志位置
\bin\Debug\net8.0\logs\20240603