log failed download tasks

This commit is contained in:
Lightczx
2024-04-29 16:43:08 +08:00
parent 6fb276af9d
commit f304e0920f
4 changed files with 52 additions and 4 deletions

View File

@@ -1,6 +1,7 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Microsoft.Extensions.Caching.Memory;
using Snap.Hutao.Core.DependencyInjection.Annotation.HttpClient;
using Snap.Hutao.Core.IO;
using Snap.Hutao.Core.IO.Hashing;
@@ -28,6 +29,7 @@ namespace Snap.Hutao.Core.Caching;
internal sealed partial class ImageCache : IImageCache, IImageCacheFilePathOperation
{
private const string CacheFolderName = nameof(ImageCache);
private const string CacheFailedDownloadTasksName = $"{nameof(ImageCache)}.FailedDownloadTasks";
private readonly FrozenDictionary<int, TimeSpan> retryCountToDelay = FrozenDictionary.ToFrozenDictionary(
[
@@ -38,10 +40,11 @@ internal sealed partial class ImageCache : IImageCache, IImageCacheFilePathOpera
private readonly ConcurrentDictionary<string, Task> concurrentTasks = new();
private readonly IHttpClientFactory httpClientFactory;
private readonly IHttpRequestMessageBuilderFactory httpRequestMessageBuilderFactory;
private readonly IHttpClientFactory httpClientFactory;
private readonly IServiceProvider serviceProvider;
private readonly ILogger<ImageCache> logger;
private readonly IMemoryCache memoryCache;
private string? baseFolder;
private string? cacheFolder;
@@ -192,6 +195,16 @@ internal sealed partial class ImageCache : IImageCache, IImageCacheFilePathOpera
if (responseMessage.IsSuccessStatusCode)
{
if (responseMessage.Content.Headers.ContentType?.MediaType is "application/json")
{
#if DEBUG
DebugTrack(uri);
#endif
string raw = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
logger.LogColorizedCritical("Failed to download '{Uri}' with unexpected body '{Raw}'", (uri, ConsoleColor.Red), (raw, ConsoleColor.DarkYellow));
return;
}
using (Stream httpStream = await responseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false))
{
using (FileStream fileStream = File.Create(baseFile))
@@ -214,10 +227,25 @@ internal sealed partial class ImageCache : IImageCache, IImageCacheFilePathOpera
}
default:
#if DEBUG
DebugTrack(uri);
#endif
logger.LogColorizedCritical("Failed to download '{Uri}' with status code '{StatusCode}'", (uri, ConsoleColor.Red), (responseMessage.StatusCode, ConsoleColor.DarkYellow));
return;
}
}
}
}
}
}
}
#if DEBUG
internal partial class ImageCache
{
private void DebugTrack(Uri uri)
{
HashSet<string>? set = memoryCache.GetOrCreate(CacheFailedDownloadTasksName, entry => entry.Value ??= new HashSet<string>()) as HashSet<string>;
set?.Add(uri.ToString());
}
}
#endif

View File

@@ -151,7 +151,7 @@ internal static class LoggerExtension
}
else
{
resultMessageBuilder.Append(ConsoleVirtualTerminalSequences.Default);
resultMessageBuilder.Append(ConsoleVirtualTerminalSequences.ForegroundWhite);
}
}
}
@@ -164,7 +164,7 @@ internal static class LoggerExtension
// Restore default colors
if (message.ForegroundColor.HasValue || message.BackgroundColor.HasValue)
{
resultMessageBuilder.Append(ConsoleVirtualTerminalSequences.Default);
resultMessageBuilder.Append(ConsoleVirtualTerminalSequences.ForegroundWhite);
}
return resultMessageBuilder.ToString();

View File

@@ -91,6 +91,13 @@
Style="{ThemeResource SettingButtonStyle}"/>
</cwc:SettingsCard>
<cwc:SettingsCard Header="[Debug Only] Print ImageCache Failed Download Tasks">
<Button
Command="{Binding DebugPrintImageCacheFailedDownloadTasksCommand}"
Content="Log"
Style="{ThemeResource SettingButtonStyle}"/>
</cwc:SettingsCard>
<cwc:SettingsCard Header="Suppress Metadata Initialization">
<ToggleSwitch IsOn="{Binding SuppressMetadataInitialization, Mode=TwoWay}"/>
</cwc:SettingsCard>

View File

@@ -1,6 +1,8 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Microsoft.Extensions.Caching.Memory;
using Snap.Hutao.Core.Caching;
using Snap.Hutao.Core.Setting;
using Snap.Hutao.Service.Notification;
using Snap.Hutao.ViewModel.Guide;
@@ -18,6 +20,8 @@ internal sealed partial class TestViewModel : Abstraction.ViewModel
{
private readonly HutaoAsAServiceClient homaAsAServiceClient;
private readonly IInfoBarService infoBarService;
private readonly ILogger<TestViewModel> logger;
private readonly IMemoryCache memoryCache;
private readonly ITaskContext taskContext;
private readonly MainWindow mainWindow;
@@ -123,4 +127,13 @@ internal sealed partial class TestViewModel : Abstraction.ViewModel
Announcement = new();
}
}
[Command("DebugPrintImageCacheFailedDownloadTasksCommand")]
private void DebugPrintImageCacheFailedDownloadTasks()
{
if (memoryCache.TryGetValue($"{nameof(ImageCache)}.FailedDownloadTasks", out HashSet<string>? set))
{
logger.LogInformation("Failed ImageCache download tasks: [{Tasks}]", set?.ToString(','));
}
}
}