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