From f7e71f598d8e0f4c5e9f62e53130ed528722e88f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E4=B9=9D?= <366193849@qq.com> Date: Fri, 10 Sep 2021 15:35:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=97=A5=E5=BF=97=E5=86=85?= =?UTF-8?q?=E5=AE=B9=E4=B8=8E=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RequestLoggingMilldeware.cs | 37 +++++++++++++++++-- FastGithub/FastGithub.csproj | 3 +- FastGithub/Program.cs | 4 +- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/FastGithub.ReverseProxy/RequestLoggingMilldeware.cs b/FastGithub.ReverseProxy/RequestLoggingMilldeware.cs index 2babb01..d08da59 100644 --- a/FastGithub.ReverseProxy/RequestLoggingMilldeware.cs +++ b/FastGithub.ReverseProxy/RequestLoggingMilldeware.cs @@ -5,6 +5,7 @@ using System; using System.Diagnostics; using System.IO; using System.Net; +using System.Text; using System.Threading.Tasks; namespace FastGithub.ReverseProxy @@ -55,19 +56,28 @@ namespace FastGithub.ReverseProxy } var exception = context.GetForwarderErrorFeature()?.Exception; - if (IsError(exception) == false) + if (exception == null) { this.logger.LogInformation(message); } - else + else if (IsError(exception)) { this.logger.LogError($"{message}{Environment.NewLine}{exception}"); } + else + { + this.logger.LogWarning($"{message}{Environment.NewLine}{GetMessage(exception)}"); + } } - private static bool IsError(Exception? exception) + /// + /// 是否为错误 + /// + /// + /// + private static bool IsError(Exception exception) { - if (exception == null || exception is OperationCanceledException) + if (exception is OperationCanceledException) { return false; } @@ -79,5 +89,24 @@ namespace FastGithub.ReverseProxy return true; } + + /// + /// 获取异常信息 + /// + /// + /// + private static string GetMessage(Exception exception) + { + var ex = exception; + var builder = new StringBuilder(); + + while (ex != null) + { + var type = ex.GetType(); + builder.Append(type.Namespace).Append(".").Append(type.Name).Append(": ").AppendLine(ex.Message); + ex = ex.InnerException; + } + return builder.ToString(); + } } } diff --git a/FastGithub/FastGithub.csproj b/FastGithub/FastGithub.csproj index 1b711e3..a833885 100644 --- a/FastGithub/FastGithub.csproj +++ b/FastGithub/FastGithub.csproj @@ -22,7 +22,8 @@ - + + diff --git a/FastGithub/Program.cs b/FastGithub/Program.cs index 484e2ce..e422d99 100644 --- a/FastGithub/Program.cs +++ b/FastGithub/Program.cs @@ -58,10 +58,12 @@ namespace FastGithub }); webBuilder.UseSerilog((hosting, logger) => { + var template = "{Timestamp:O} [{Level:u3}]{NewLine}{SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}"; logger .ReadFrom.Configuration(hosting.Configuration) .Enrich.FromLogContext() - .WriteTo.Console(outputTemplate: "{Timestamp:O} [{Level:u3}]{NewLine}{SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}"); + .WriteTo.Console(outputTemplate: template) + .WriteTo.File(Path.Combine("logs", @"log.txt"), rollingInterval: RollingInterval.Day, outputTemplate: template); }); }); }