Add request logging and include complete % in the logs.

This commit is contained in:
Roger Far 2023-06-11 13:42:14 -06:00
parent 8418b26d70
commit 7732649b4b
7 changed files with 80 additions and 7 deletions

3
.gitignore vendored
View file

@ -6,4 +6,5 @@ node_modules/
RealDebridClient.zip
server/RdtClient.Web/appsettings.Development.json
data
Dockerfile.dev
Dockerfile.dev
test.bat

View file

@ -14,6 +14,6 @@ public class UserData
public async Task<IdentityUser?> GetUser()
{
return await _dataContext.Users.FirstOrDefaultAsync();
return await _dataContext.Users.OrderBy(m => m.Id).FirstOrDefaultAsync();
}
}

View file

@ -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:
<a href=""https://real-debrid.com/?id=1348683"" target=""_blank"" rel=""noopener"">https://real-debrid.com</a>
<a href=""https://alldebrid.com/?uid=2v91l&lang=en"" target=""_blank"" rel=""noopener"">https://alldebrid.com</a>
<a href=""https://www.premiumize.me/"" target=""_blank"" rel=""noopener"">https://www.premiumize.me/</a>

View file

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

View file

@ -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<RequestLoggingMiddleware>();
}
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<String> 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;
}
}

View file

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

View file

@ -169,6 +169,8 @@ try
app.UseMiddleware<BaseHrefMiddleware>();
}
app.UseMiddleware<RequestLoggingMiddleware>();
app.UseRouting();
app.UseAuthentication();