From 7732649b4bde2eeec0277df68d459f56c4fa3153 Mon Sep 17 00:00:00 2001 From: Roger Far Date: Sun, 11 Jun 2023 13:42:14 -0600 Subject: [PATCH] Add request logging and include complete % in the logs. --- .gitignore | 3 +- server/RdtClient.Data/Data/UserData.cs | 2 +- .../Models/Internal/DbSettings.cs | 2 +- server/RdtClient.Service/Helpers/Logger.cs | 2 +- .../Middleware/RequestLoggingMiddleware.cs | 60 +++++++++++++++++++ .../Services/TorrentRunner.cs | 16 ++++- server/RdtClient.Web/Program.cs | 2 + 7 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 server/RdtClient.Service/Middleware/RequestLoggingMiddleware.cs diff --git a/.gitignore b/.gitignore index aa1877b..a4c0484 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ node_modules/ RealDebridClient.zip server/RdtClient.Web/appsettings.Development.json data -Dockerfile.dev \ No newline at end of file +Dockerfile.dev +test.bat diff --git a/server/RdtClient.Data/Data/UserData.cs b/server/RdtClient.Data/Data/UserData.cs index 41c2702..3d3e37e 100644 --- a/server/RdtClient.Data/Data/UserData.cs +++ b/server/RdtClient.Data/Data/UserData.cs @@ -14,6 +14,6 @@ public class UserData public async Task GetUser() { - return await _dataContext.Users.FirstOrDefaultAsync(); + return await _dataContext.Users.OrderBy(m => m.Id).FirstOrDefaultAsync(); } } \ No newline at end of file diff --git a/server/RdtClient.Data/Models/Internal/DbSettings.cs b/server/RdtClient.Data/Models/Internal/DbSettings.cs index 827310e..24ae0a2 100644 --- a/server/RdtClient.Data/Models/Internal/DbSettings.cs +++ b/server/RdtClient.Data/Models/Internal/DbSettings.cs @@ -116,7 +116,7 @@ http://127.0.0.1:6800/jsonrpc.")] public class DbSettingsProvider { [DisplayName("Provider")] - [Description(@"The following 2 providers are supported: + [Description(@"The following 3 providers are supported: https://real-debrid.com https://alldebrid.com https://www.premiumize.me/ diff --git a/server/RdtClient.Service/Helpers/Logger.cs b/server/RdtClient.Service/Helpers/Logger.cs index 09f1e45..ea3b503 100644 --- a/server/RdtClient.Service/Helpers/Logger.cs +++ b/server/RdtClient.Service/Helpers/Logger.cs @@ -16,7 +16,7 @@ public static class Logger fileName = HttpUtility.UrlDecode(fileName); } - var done = (Int32)((Double)download.BytesDone / download.BytesTotal) * 100; + var done = (Int32)((Double)download.BytesDone / download.BytesTotal * 100); return $"for download {fileName} {done}% ({download.DownloadId})"; } diff --git a/server/RdtClient.Service/Middleware/RequestLoggingMiddleware.cs b/server/RdtClient.Service/Middleware/RequestLoggingMiddleware.cs new file mode 100644 index 0000000..395e039 --- /dev/null +++ b/server/RdtClient.Service/Middleware/RequestLoggingMiddleware.cs @@ -0,0 +1,60 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using System.Text; + +namespace RdtClient.Service.Middleware; + +public class RequestLoggingMiddleware +{ + private readonly RequestDelegate _next; + private readonly ILogger _logger; + + public RequestLoggingMiddleware(RequestDelegate next, ILoggerFactory loggerFactory) + { + _next = next; + _logger = loggerFactory.CreateLogger(); + } + + public async Task Invoke(HttpContext context) + { + if (!_logger.IsEnabled(LogLevel.Debug) || (!context.Request.Path.StartsWithSegments("/api/v2") && !context.Request.Path.StartsWithSegments("/api/torrents"))) + { + await _next(context); + + return; + } + + var requestLog = $"Method: {context.Request.Method}, Path: {context.Request.Path}"; + + if (context.Request.QueryString.HasValue) + { + requestLog += $", QueryString: {context.Request.QueryString}"; + } + + if (context.Request.HasFormContentType && context.Request.Form.Any()) + { + requestLog += $", Form: {String.Join(", ", context.Request.Form.Select(f => $"{f.Key}: {f.Value}"))}"; + } + else if (context.Request.ContentType?.ToLower().Contains("application/json") == true) + { + var body = await ReadRequestBodyAsync(context.Request); + requestLog += $", Body: {body}"; + } + + _logger.LogDebug(requestLog); + + await _next(context); + } + + private static async Task ReadRequestBodyAsync(HttpRequest request) + { + request.EnableBuffering(); + + using var reader = new StreamReader(request.Body, Encoding.UTF8, detectEncodingFromByteOrderMarks: false, leaveOpen: true); + var body = await reader.ReadToEndAsync(); + + request.Body.Position = 0; + + return body; + } +} diff --git a/server/RdtClient.Service/Services/TorrentRunner.cs b/server/RdtClient.Service/Services/TorrentRunner.cs index 0cfcc9d..593629f 100644 --- a/server/RdtClient.Service/Services/TorrentRunner.cs +++ b/server/RdtClient.Service/Services/TorrentRunner.cs @@ -512,9 +512,19 @@ public class TorrentRunner if ((torrent.Downloads.Count > 0) || torrent.RdStatus == TorrentStatus.Finished && torrent.HostDownloadAction == TorrentHostDownloadAction.DownloadNone) { - var allComplete = torrent.Downloads.Count(m => m.Completed != null); + var completeCount = torrent.Downloads.Count(m => m.Completed != null); - if (allComplete == torrent.Downloads.Count) + var completePerc = 0; + + var totalDownloadBytes = torrent.Downloads.Sum(m => m.BytesTotal); + var totalDoneBytes = torrent.Downloads.Sum(m => m.BytesDone); + + if (totalDownloadBytes > 0) + { + completePerc = (Int32)((Double)totalDoneBytes / totalDownloadBytes * 100); + } + + if (completeCount == torrent.Downloads.Count) { Log($"All downloads complete, marking torrent as complete", torrent); @@ -558,7 +568,7 @@ public class TorrentRunner } else { - Log($"Waiting for downloads to complete. {allComplete}/{torrent.Downloads.Count} complete", torrent); + Log($"Waiting for downloads to complete. {completeCount}/{torrent.Downloads.Count} complete ({completePerc}%)", torrent); } } } diff --git a/server/RdtClient.Web/Program.cs b/server/RdtClient.Web/Program.cs index ecf788e..f20a4e5 100644 --- a/server/RdtClient.Web/Program.cs +++ b/server/RdtClient.Web/Program.cs @@ -169,6 +169,8 @@ try app.UseMiddleware(); } + app.UseMiddleware(); + app.UseRouting(); app.UseAuthentication();