From 040b6ff6911c34524f0a3fd46942393e2329b089 Mon Sep 17 00:00:00 2001 From: Jordan Fearnley Date: Sun, 12 Nov 2023 20:15:03 +0000 Subject: [PATCH] Misc: Logging changes, dependency update --- Helpers/DiscordHelper.cs | 8 ++++++-- Helpers/ShokoHelper.cs | 35 ++++++++++++++++++++--------------- WebhookDump.cs | 4 ++-- WebhookDump.csproj | 2 +- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/Helpers/DiscordHelper.cs b/Helpers/DiscordHelper.cs index f762add..714cb47 100644 --- a/Helpers/DiscordHelper.cs +++ b/Helpers/DiscordHelper.cs @@ -46,6 +46,8 @@ public async Task SendWebhook(IVideoFile file, string dumpResult, AniDBS { Webhook webhook = GetUnmatchedWebhook(file, dumpResult, searchResult); + _logger.Info(CultureInfo.InvariantCulture, "Sending Discord webhook (fileId={fileId})", file.VideoFileID); + HttpResponseMessage response = await _httpClient.PostAsJsonAsync($"{BaseUrl}?wait=true", webhook, _options); _ = response.EnsureSuccessStatusCode(); @@ -57,13 +59,15 @@ public async Task SendWebhook(IVideoFile file, string dumpResult, AniDBS catch (Exception ex) { // TODO: More logging - _logger.Warn("Exception: {ex}", ex); + _logger.Debug("Exception: {ex}", ex); return null; } } public async Task PatchWebhook(IVideoFile file, IAnime anime, IEpisode episode, MemoryStream imageStream, string messageId) { + _logger.Info(CultureInfo.InvariantCulture, "Attempting to update Discord message (fileId={fileId}, messageId={messageId})", file.VideoFileID, messageId); + try { MultipartFormDataContent form = new(); @@ -89,7 +93,7 @@ public async Task PatchWebhook(IVideoFile file, IAnime anime, IEpisode episode, catch (Exception ex) { // TODO: More logging - _logger.Warn("Exception: {ex}", ex); + _logger.Debug("Exception: {ex}", ex); } } diff --git a/Helpers/ShokoHelper.cs b/Helpers/ShokoHelper.cs index dfa235f..dfa63bc 100644 --- a/Helpers/ShokoHelper.cs +++ b/Helpers/ShokoHelper.cs @@ -1,8 +1,9 @@ using System; +using System.Globalization; using System.IO; using System.Net; using System.Net.Http; -using System.Text; +using System.Net.Http.Json; using System.Text.Json; using System.Text.RegularExpressions; using System.Threading.Tasks; @@ -40,19 +41,19 @@ public async Task DumpFile(int fileId) { try { - var requestObject = new + _logger.Info(CultureInfo.InvariantCulture, "Plugin triggering automatic AVDump (fileId={fileId})", fileId); + + HttpResponseMessage response = await _httpClient.PostAsJsonAsync("AVDump/DumpFiles", new { FileIDs = new[] { fileId }, Priority = false - }; - var json = new StringContent(JsonSerializer.Serialize(requestObject), Encoding.UTF8, "application/json"); - - HttpResponseMessage response = await _httpClient.PostAsync("AVDump/DumpFiles", json); + }); _ = response.EnsureSuccessStatusCode(); } catch (Exception ex) { - _logger.Warn("Exception: {ex}", ex); + _logger.Warn(CultureInfo.InvariantCulture, "Failed to process AVDump request (fileId={fileId})", fileId); + _logger.Debug("Exception: {ex}", ex); } } @@ -72,8 +73,8 @@ public async Task MatchTitle(string filename) ex is HttpRequestException or JsonException or ArgumentNullException or InvalidOperationException ) { - _logger.Warn($"Unable to retrieve information about the file ('{filename}') from AniDB"); - _logger.Warn("Exception: {ex}", ex); + _logger.Warn(CultureInfo.InvariantCulture, "Unable to retrieve title information for a file (fileName='{filename}') from AniDB", filename); + _logger.Debug("Exception: {ex}", ex); return null; } } @@ -84,6 +85,8 @@ public async Task ScanFile(IVideoFile file, int autoMatchAttempts = 1) { await Task.Delay(TimeSpan.FromMinutes(autoMatchAttempts * 5)); + _logger.Info(CultureInfo.InvariantCulture, "Requesting file rescan (fileId={fileID}, matchAttempts={matchAttempts})", file.VideoFileID, autoMatchAttempts); + HttpResponseMessage response = await _httpClient.PostAsync($"File/{file.VideoFileID}/Rescan", null); _ = response.EnsureSuccessStatusCode(); } @@ -98,12 +101,14 @@ public async Task ScanFileById(int fileId) { try { + _logger.Info(CultureInfo.InvariantCulture, "Requesting file rescan (fileId={fileID})", fileId); + HttpResponseMessage response = await _httpClient.PostAsync($"File/{fileId}/Rescan", null); _ = response.EnsureSuccessStatusCode(); } catch (HttpRequestException ex) { - _logger.Warn($"Unable to scan file by ID ('{fileId}')"); + _logger.Warn(CultureInfo.InvariantCulture, "Unable to scan file by ID ('{fileId}')", fileId); _logger.Warn("Exception: ", ex); } } @@ -117,14 +122,14 @@ public async Task GetSeriesPoster(IAnime anime) using Stream responseStream = await response.Content.ReadAsStreamAsync(); using JsonDocument jsonDoc = await JsonDocument.ParseAsync(responseStream); - + string image = jsonDoc.RootElement.GetProperty("Images").GetProperty("Posters")[0].GetRawText(); return JsonSerializer.Deserialize(image); } catch (HttpRequestException ex) { - _logger.Warn($"Poster could not be downloaded for series ID: {anime.AnimeID}"); - _logger.Warn("Exception: {ex}", ex); + _logger.Warn(CultureInfo.InvariantCulture, "Poster could not be downloaded for series ID: {animeId}", anime.AnimeID); + _logger.Debug("Exception: {ex}", ex); return null; } } @@ -146,8 +151,8 @@ public async Task GetImageStream(AniDBPoster poster) } catch (HttpRequestException ex) { - _logger.Warn($"Could not retreieve image for the primary {poster.Source} {poster.Type} of {poster.ID}"); - _logger.Warn("Exception: {ex}", ex); + _logger.Warn(CultureInfo.InvariantCulture, "Could not retreieve image for the primary {poster.Source} {poster.Type} of {poster.ID}", poster.Source, poster.Type, poster.ID); + _logger.Debug("Exception: {ex}", ex); return null; } } diff --git a/WebhookDump.cs b/WebhookDump.cs index 8b9f093..6510391 100644 --- a/WebhookDump.cs +++ b/WebhookDump.cs @@ -94,7 +94,7 @@ private void OnFileNotMatched(object sender, FileNotMatchedEventArgs fileNotMatc } catch (Exception ex) { - _logger.Warn("Exception: {ex}", ex); + _logger.Debug("Exception: {ex}", ex); } } } @@ -125,7 +125,7 @@ private async void OnFileMatched(object sender, FileMatchedEventArgs fileMatched } catch (Exception ex) { - _logger.Warn("Exception: {ex}", ex); + _logger.Debug("Exception: {ex}", ex); } } diff --git a/WebhookDump.csproj b/WebhookDump.csproj index 598f32b..5dfa203 100644 --- a/WebhookDump.csproj +++ b/WebhookDump.csproj @@ -23,7 +23,7 @@ all - + all