当前位置: 代码迷 >> 综合 >> .Net Core WebApi 中集成Log4Net 日志记录
  详细解决方案

.Net Core WebApi 中集成Log4Net 日志记录

热度:77   发布时间:2023-12-15 00:30:12.0

.Net Core WebApi 中集成Log4Net 日志记录

  • 1.安装Log4Net的 NuGet 包:
  • 2.Log4Net的配置文件的内容:
  • 3.在Program中注册Log4Get
  • 4. 在controller中测试
  • 5.日志记录结果

1.安装Log4Net的 NuGet 包:

安装时只需要安装这一个Nuget包即可 其它依赖的包会自动安装到项目。
安装命令:insatll-package Microsoft.Extensions.Logging.Log4Net.AspNetCore 3.0.0 (也可用NuGet管理器安装)!

2.Log4Net的配置文件的内容:

<?xml version="1.0" encoding="utf-8" ?>
<log4net><!-- 日志的等级,它们由高到底分别为: OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL --><appender name="infoAppender" type="log4net.Appender.RollingFileAppender"><param name="File" value="Logs\Info\info.log" /><param name="AppendToFile" value="true" /><param name="MaxFileSize" value="10240" /><param name="MaxSizeRollBackups" value="100" /><param name="PreserveLogFileNameExtension" value="true" /><param name="StaticLogFileName" value="false" /><param name="DatePattern" value="yyyyMMdd" /><param name="RollingStyle" value="Date" /><layout type="log4net.Layout.PatternLayout"><conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /></layout><filter type="log4net.Filter.LevelRangeFilter"><param name="LevelMin" value="INFO" /><param name="LevelMax" value="INFO" /></filter></appender><appender name="debugAppender" type="log4net.Appender.RollingFileAppender"><param name="File" value="Logs\Debug\debug.log" /><param name="AppendToFile" value="true" /><param name="MaxFileSize" value="10240" /><param name="MaxSizeRollBackups" value="100" /><param name="PreserveLogFileNameExtension" value="true" /><param name="StaticLogFileName" value="false" /><param name="DatePattern" value="yyyyMMdd" /><param name="RollingStyle" value="Date" /><layout type="log4net.Layout.PatternLayout"><conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /></layout><filter type="log4net.Filter.LevelRangeFilter"><param name="LevelMin" value="DEBUG" /><param name="LevelMax" value="DEBUG" /></filter></appender><appender name="errorAppender" type="log4net.Appender.RollingFileAppender"><param name="File" value="Logs\Error\Err.log" /><param name="AppendToFile" value="true" /><param name="MaxFileSize" value="10240" /><param name="MaxSizeRollBackups" value="100" /><param name="PreserveLogFileNameExtension" value="true" /><param name="StaticLogFileName" value="false" /><param name="DatePattern" value="yyyyMMdd" /><param name="RollingStyle" value="Date" /><layout type="log4net.Layout.PatternLayout"><conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /></layout><filter type="log4net.Filter.LevelRangeFilter"><param name="LevelMin" value="ERROR" /><param name="LevelMax" value="ERROR" /></filter></appender><appender name="warnAppender" type="log4net.Appender.RollingFileAppender"><param name="File" value="Logs\Warn\Warn.log" /><param name="AppendToFile" value="true" /><param name="MaxFileSize" value="10240" /><param name="MaxSizeRollBackups" value="100" /><param name="PreserveLogFileNameExtension" value="true" /><param name="StaticLogFileName" value="false" /><param name="DatePattern" value="yyyyMMdd" /><param name="RollingStyle" value="Date" /><layout type="log4net.Layout.PatternLayout"><conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /></layout><filter type="log4net.Filter.LevelRangeFilter"><param name="LevelMin" value="WARN" /><param name="LevelMax" value="WARN" /></filter></appender><appender name="fatalAppender" type="log4net.Appender.RollingFileAppender"><param name="File" value="Logs\Fatal\Fatal.log" /><param name="AppendToFile" value="true" /><param name="MaxFileSize" value="10240" /><param name="MaxSizeRollBackups" value="100" /><param name="PreserveLogFileNameExtension" value="true" /><param name="StaticLogFileName" value="false" /><param name="DatePattern" value="yyyyMMdd" /><param name="RollingStyle" value="Date" /><layout type="log4net.Layout.PatternLayout"><conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /></layout><filter type="log4net.Filter.LevelRangeFilter"><param name="LevelMin" value="FATAL" /><param name="LevelMax" value="FATAL" /></filter></appender><root><level value="ALL" ref="infoAppender" /><appender-ref ref="infoAppender" /><appender-ref ref="errorAppender" /><appender-ref ref="debugAppender" /><appender-ref ref="warnAppender" /><appender-ref ref="fatalAppender" /></root></log4net>

3.在Program中注册Log4Get

 public static IHostBuilder CreateHostBuilder(string[] args) =>Host.CreateDefaultBuilder(args).ConfigureLogging(logbuilder =>{logbuilder.AddFilter("System", LogLevel.Warning);logbuilder.AddFilter("Microsoft", LogLevel.Warning);logbuilder.SetMinimumLevel(LogLevel.Warning);logbuilder.AddLog4Net();}).ConfigureWebHostDefaults(webBuilder =>{webBuilder.UseStartup<Startup>();});

4. 在controller中测试

private readonly ILogger<UpdateFilesController> _logger;private readonly IUpdateFiles _updateFiles;public UpdateFilesController(ILogger<UpdateFilesController> logger,IUpdateFiles updateFiles){_logger = logger;_updateFiles = updateFiles;}/// <summary>/// 获取要更新的版本信息/// </summary>/// <returns></returns>[HttpGet]public List<FilesInfo> InspectVersion(){return _updateFiles.InspectVersion();}/// <summary>/// 获取要更新的全部文件信息/// </summary>/// <param name="version"></param>/// <returns></returns>[HttpGet]public List<FilesInfo> GetUpdateFiles(long version, bool isforce){_logger.LogInformation("测试日志");return _updateFiles.GetUpdateFiles(version, isforce);}

5.日志记录结果

在这里插入图片描述
可以看到在我们的测试日志就已经记录成功了! 快去实测吧!

  相关解决方案