日志参数化
This commit is contained in:
parent
fe30df3fe5
commit
9084d07434
68
FastGithub.Configuration/LoggerExtensions.cs
Normal file
68
FastGithub.Configuration/LoggerExtensions.cs
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace FastGithub
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 日志插值字符串扩展
|
||||||
|
/// </summary>
|
||||||
|
public static class LoggerExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 输出日志
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logger"></param>
|
||||||
|
/// <param name="level"></param>
|
||||||
|
/// <param name="formattableString"></param>
|
||||||
|
public static void Log(this ILogger logger, LogLevel level, FormattableString formattableString)
|
||||||
|
=> logger.Log(level, formattableString.Format, formattableString.GetArguments());
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 输出Trace日志
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logger"></param>
|
||||||
|
/// <param name="formattableString"></param>
|
||||||
|
public static void LogTrace(this ILogger logger, FormattableString formattableString)
|
||||||
|
=> logger.Log(LogLevel.Trace, formattableString);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 输出Debug日志
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logger"></param>
|
||||||
|
/// <param name="formattableString"></param>
|
||||||
|
public static void LogDebug(this ILogger logger, FormattableString formattableString)
|
||||||
|
=> logger.Log(LogLevel.Debug, formattableString);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 输出Information日志
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logger"></param>
|
||||||
|
/// <param name="formattableString"></param>
|
||||||
|
public static void LogInformation(this ILogger logger, FormattableString formattableString)
|
||||||
|
=> logger.Log(LogLevel.Information, formattableString);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 输出Warning日志
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logger"></param>
|
||||||
|
/// <param name="formattableString"></param>
|
||||||
|
public static void LogWarning(this ILogger logger, FormattableString formattableString)
|
||||||
|
=> logger.Log(LogLevel.Warning, formattableString);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 输出日志
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logger"></param>
|
||||||
|
/// <param name="formattableString"></param>
|
||||||
|
public static void LogError(this ILogger logger, FormattableString formattableString)
|
||||||
|
=> logger.Log(LogLevel.Error, formattableString);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 输出Critical日志
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logger"></param>
|
||||||
|
/// <param name="formattableString"></param>
|
||||||
|
public static void LogCritical(this ILogger logger, FormattableString formattableString)
|
||||||
|
=> logger.Log(LogLevel.Critical, formattableString);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -51,20 +51,19 @@ namespace FastGithub.HttpServer
|
|||||||
}
|
}
|
||||||
|
|
||||||
var request = context.Request;
|
var request = context.Request;
|
||||||
var response = context.Response;
|
var response = context.Response;
|
||||||
var message = $"{request.Method} {request.Scheme}://{request.Host}{request.Path} responded {response.StatusCode} in {stopwatch.Elapsed.TotalMilliseconds} ms";
|
|
||||||
var exception = context.GetForwarderErrorFeature()?.Exception;
|
var exception = context.GetForwarderErrorFeature()?.Exception;
|
||||||
if (exception == null)
|
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))
|
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
|
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)}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user