NETCORE - 日志插件 Microsoft.Extensions.Logging
netcore的默认日志插件为 Microsoft.Extensions.Logging,已集成在框架中。
使用样例:
namespace RailGraph.Controllers { [ApiController] [Route("[controller]")] public class ANeo4jController : ControllerBase { public ILogger<ANeo4jController> logger; public ANeo4jController(ILogger<ANeo4jController> _logger) { this.logger = _logger; } [HttpGet] [Route("GetModelDataAsync")] public async Task<ActionResult> GetModelDataAsync(int fileId) { try { logger.LogInformation($"接口进入调用:GetModelDataAsync,参数:{fileId}"); logger.LogInformation($"接口调用完成,成功返回。"); return Ok(""); } catch (Exception ex) { return BadRequest(ex.Message + ex.InnerException?.Message); } } } }
如需要把日志写到文件中。
则需要增加插件:
Karambolo.Extensions.Logging.File
在项目的Program.cs中配置
#region == 默认的 Microsoft.Extensions.Logging 日志 == var logRootPath = AppContext.BaseDirectory; var logFiles = new[] { new LogFileOptions { Path = $"logs/log-{DateTime.Now.ToString("yyyyMMdd")}.txt" } }; builder.Logging.AddConsole(); builder.Logging.AddFile(options => { //options.DateFormat = "yyyyMMdd"; //配置文件根目录 options.RootPath = logRootPath; //配置文件名称 options.Files = logFiles; }); #endregion
end.
标签:插件,Logging,NETCORE,Microsoft,Extensions,日志,logger From: https://www.cnblogs.com/1285026182YUAN/p/18459854