From 9084d074349bb11645eef88ae14395abe6cb03d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Wed, 12 Jan 2022 16:10:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=8F=82=E6=95=B0=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Configuration/LoggerExtensions.cs | 68 +++++++++++++++++++ .../RequestLoggingMilldeware.cs | 9 ++- 2 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 FastGithub.Configuration/LoggerExtensions.cs diff --git a/FastGithub.Configuration/LoggerExtensions.cs b/FastGithub.Configuration/LoggerExtensions.cs new file mode 100644 index 0000000..5d5ba95 --- /dev/null +++ b/FastGithub.Configuration/LoggerExtensions.cs @@ -0,0 +1,68 @@ +using Microsoft.Extensions.Logging; +using System; + +namespace FastGithub +{ + /// + /// 日志插值字符串扩展 + /// + public static class LoggerExtensions + { + /// + /// 输出日志 + /// + /// + /// + /// + public static void Log(this ILogger logger, LogLevel level, FormattableString formattableString) + => logger.Log(level, formattableString.Format, formattableString.GetArguments()); + + /// + /// 输出Trace日志 + /// + /// + /// + public static void LogTrace(this ILogger logger, FormattableString formattableString) + => logger.Log(LogLevel.Trace, formattableString); + + /// + /// 输出Debug日志 + /// + /// + /// + public static void LogDebug(this ILogger logger, FormattableString formattableString) + => logger.Log(LogLevel.Debug, formattableString); + + /// + /// 输出Information日志 + /// + /// + /// + public static void LogInformation(this ILogger logger, FormattableString formattableString) + => logger.Log(LogLevel.Information, formattableString); + + /// + /// 输出Warning日志 + /// + /// + /// + public static void LogWarning(this ILogger logger, FormattableString formattableString) + => logger.Log(LogLevel.Warning, formattableString); + + /// + /// 输出日志 + /// + /// + /// + public static void LogError(this ILogger logger, FormattableString formattableString) + => logger.Log(LogLevel.Error, formattableString); + + /// + /// 输出Critical日志 + /// + /// + /// + public static void LogCritical(this ILogger logger, FormattableString formattableString) + => logger.Log(LogLevel.Critical, formattableString); + } +} diff --git a/FastGithub.HttpServer/RequestLoggingMilldeware.cs b/FastGithub.HttpServer/RequestLoggingMilldeware.cs index bdf7311..0450058 100644 --- a/FastGithub.HttpServer/RequestLoggingMilldeware.cs +++ b/FastGithub.HttpServer/RequestLoggingMilldeware.cs @@ -51,20 +51,19 @@ namespace FastGithub.HttpServer } var request = context.Request; - var response = context.Response; - var message = $"{request.Method} {request.Scheme}://{request.Host}{request.Path} responded {response.StatusCode} in {stopwatch.Elapsed.TotalMilliseconds} ms"; + var response = context.Response; var exception = context.GetForwarderErrorFeature()?.Exception; if (exception == null) { - this.logger.LogInformation(message); + this.logger.LogInformation($"{request.Method} {request.Scheme}://{request.Host}{request.Path} responded {response.StatusCode} in {stopwatch.Elapsed.TotalMilliseconds} ms"); } else if (IsError(exception)) { - this.logger.LogError($"{message}{Environment.NewLine}{exception}"); + this.logger.LogError($"{request.Method} {request.Scheme}://{request.Host}{request.Path} responded {response.StatusCode} in {stopwatch.Elapsed.TotalMilliseconds} ms{Environment.NewLine}{exception}"); } else { - this.logger.LogWarning($"{message}{Environment.NewLine}{GetMessage(exception)}"); + this.logger.LogWarning($"{request.Method} {request.Scheme}://{request.Host}{request.Path} responded {response.StatusCode} in {stopwatch.Elapsed.TotalMilliseconds} ms{Environment.NewLine}{GetMessage(exception)}"); } }