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); }); }); }