mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-07-09 03:04:24 -04:00
Merge pull request #7913 from cvium/fix_response_logging
(cherry picked from commit 42aaea3556c3c332af1d2083547270ebce74ca31) Signed-off-by: crobibero <cody@robibe.ro>
This commit is contained in:
parent
b6a0fe3378
commit
758b16691f
@ -19,41 +19,44 @@ namespace Jellyfin.Server.Middleware
|
|||||||
private readonly RequestDelegate _next;
|
private readonly RequestDelegate _next;
|
||||||
private readonly ILogger<ResponseTimeMiddleware> _logger;
|
private readonly ILogger<ResponseTimeMiddleware> _logger;
|
||||||
|
|
||||||
private readonly bool _enableWarning;
|
|
||||||
private readonly long _warningThreshold;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="ResponseTimeMiddleware"/> class.
|
/// Initializes a new instance of the <see cref="ResponseTimeMiddleware"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="next">Next request delegate.</param>
|
/// <param name="next">Next request delegate.</param>
|
||||||
/// <param name="logger">Instance of the <see cref="ILogger{ExceptionMiddleware}"/> interface.</param>
|
/// <param name="logger">Instance of the <see cref="ILogger{ExceptionMiddleware}"/> interface.</param>
|
||||||
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
|
|
||||||
public ResponseTimeMiddleware(
|
public ResponseTimeMiddleware(
|
||||||
RequestDelegate next,
|
RequestDelegate next,
|
||||||
ILogger<ResponseTimeMiddleware> logger,
|
ILogger<ResponseTimeMiddleware> logger)
|
||||||
IServerConfigurationManager serverConfigurationManager)
|
|
||||||
{
|
{
|
||||||
_next = next;
|
_next = next;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
|
||||||
_enableWarning = serverConfigurationManager.Configuration.EnableSlowResponseWarning;
|
|
||||||
_warningThreshold = serverConfigurationManager.Configuration.SlowResponseThresholdMs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Invoke request.
|
/// Invoke request.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="context">Request context.</param>
|
/// <param name="context">Request context.</param>
|
||||||
|
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
|
||||||
/// <returns>Task.</returns>
|
/// <returns>Task.</returns>
|
||||||
public async Task Invoke(HttpContext context)
|
public async Task Invoke(HttpContext context, IServerConfigurationManager serverConfigurationManager)
|
||||||
{
|
{
|
||||||
var watch = new Stopwatch();
|
var watch = new Stopwatch();
|
||||||
watch.Start();
|
watch.Start();
|
||||||
|
var enableWarning = serverConfigurationManager.Configuration.EnableSlowResponseWarning;
|
||||||
|
var warningThreshold = serverConfigurationManager.Configuration.SlowResponseThresholdMs;
|
||||||
context.Response.OnStarting(() =>
|
context.Response.OnStarting(() =>
|
||||||
{
|
{
|
||||||
watch.Stop();
|
watch.Stop();
|
||||||
LogWarning(context, watch);
|
if (enableWarning && watch.ElapsedMilliseconds > warningThreshold)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(
|
||||||
|
"Slow HTTP Response from {Url} to {RemoteIp} in {Elapsed:g} with Status Code {StatusCode}",
|
||||||
|
context.Request.GetDisplayUrl(),
|
||||||
|
context.GetNormalizedRemoteIp(),
|
||||||
|
watch.Elapsed,
|
||||||
|
context.Response.StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
var responseTimeForCompleteRequest = watch.ElapsedMilliseconds;
|
var responseTimeForCompleteRequest = watch.ElapsedMilliseconds;
|
||||||
context.Response.Headers[ResponseHeaderResponseTime] = responseTimeForCompleteRequest.ToString(CultureInfo.InvariantCulture);
|
context.Response.Headers[ResponseHeaderResponseTime] = responseTimeForCompleteRequest.ToString(CultureInfo.InvariantCulture);
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
@ -62,18 +65,5 @@ namespace Jellyfin.Server.Middleware
|
|||||||
// Call the next delegate/middleware in the pipeline
|
// Call the next delegate/middleware in the pipeline
|
||||||
await this._next(context).ConfigureAwait(false);
|
await this._next(context).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void LogWarning(HttpContext context, Stopwatch watch)
|
|
||||||
{
|
|
||||||
if (_enableWarning && watch.ElapsedMilliseconds > _warningThreshold)
|
|
||||||
{
|
|
||||||
_logger.LogWarning(
|
|
||||||
"Slow HTTP Response from {Url} to {RemoteIp} in {Elapsed:g} with Status Code {StatusCode}",
|
|
||||||
context.Request.GetDisplayUrl(),
|
|
||||||
context.GetNormalizedRemoteIp(),
|
|
||||||
watch.Elapsed,
|
|
||||||
context.Response.StatusCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user