From 0da9301bd9bb52300a2e017e36544e1223b39e47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=91=E6=9E=97=E5=AF=BA=E9=A9=BB=E6=AD=A6=E5=BD=93?= =?UTF-8?q?=E5=B1=B1=E5=8A=9E=E4=BA=8B=E5=A4=84=E5=A4=A7=E7=A5=9E=E7=88=B6?= =?UTF-8?q?=E7=8E=8B=E5=96=87=E5=98=9B?= <7342044+huguodong520@user.noreply.gitee.com> Date: Fri, 29 Jul 2022 07:03:11 +0000 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=B0=81=E8=A3=85=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/Magic.Web.Core/Startup.cs | 66 ++++++++++++++++--------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/backend/Magic.Web.Core/Startup.cs b/backend/Magic.Web.Core/Startup.cs index 185c8bd..8e99b2b 100644 --- a/backend/Magic.Web.Core/Startup.cs +++ b/backend/Magic.Web.Core/Startup.cs @@ -16,6 +16,7 @@ using System.Text.Json; using System.Text.Json.Serialization; using System.Text.Unicode; using Yitter.IdGenerator; +using Furion.Templates; namespace Magic.Web.Core; @@ -97,49 +98,26 @@ public class Startup : AppStartup builder.AddSubscriber(); }); - // 日志记录 + // 日志记录 + // 错误级别日志归类 + // 每天创建一个日志文件 services.AddLogging(builder => { - // 错误级别日志归类 + + builder.AddFile("logs/error/{0:yyyyMMdd}_.log", options => { - options.WriteFilter = (logMsg) => - { - return logMsg.LogLevel == LogLevel.Error; - }; - options.FileNameRule = fileName => - { - return string.Format(fileName, DateTime.UtcNow); - }; - options.FileSizeLimitBytes = 500 * 1024; + SetLogOptions(options, LogLevel.Error); }); - // 每天创建一个日志文件 builder.AddFile("logs/info/{0:yyyyMMdd}_.log", options => { - options.WriteFilter = (logMsg) => - { - return logMsg.LogLevel == LogLevel.Information; - }; - options.FileNameRule = fileName => - { - return string.Format(fileName, DateTime.UtcNow); - }; - options.FileSizeLimitBytes = 500 * 1024; + SetLogOptions(options, LogLevel.Information); }); builder.AddFile("logs/warn/{0:yyyyMMdd}_.log", options => { - options.WriteFilter = (logMsg) => - { - return logMsg.LogLevel == LogLevel.Warning; - }; - options.FileNameRule = fileName => - { - return string.Format(fileName, DateTime.UtcNow); - }; - options.FileSizeLimitBytes = 500 * 1024; + SetLogOptions(options, LogLevel.Warning); }); }); - } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) @@ -180,4 +158,30 @@ public class Startup : AppStartup }); } + + private void SetLogOptions(FileLoggerOptions options, LogLevel logLevel) + { + options.WriteFilter = (logMsg) => + { + return logMsg.LogLevel == logLevel; + }; + options.FileNameRule = fileName => + { + return string.Format(fileName, DateTime.UtcNow); + }; + options.FileSizeLimitBytes = 500 * 1024; + options.MessageFormat = (logMsg) => + { + List msg = new List() { + $"##日志时间## {DateTime.Now.ToString("O")}", + $"##日志等级## {logLevel}", + $"##日志内容## {logMsg.Message}", + }; + if (!string.IsNullOrEmpty(logMsg.Exception?.ToString())) msg.Add($"##异常信息## {logMsg.Exception}"); + + // 生成模板字符串 + var template = TP.Wrapper($"{logMsg.LogName}", "", msg.ToArray()); + return template; + }; + } } -- Gitee