From 9bdb5f364c726398d1f776a291d4d26746fdde82 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Sun, 2 Jul 2023 10:21:33 -0500 Subject: [PATCH] refactor: Improve logging for caching logic --- src/Recyclarr.Cli/Cache/CachePersister.cs | 5 ++--- src/Recyclarr.Cli/Cache/ServiceCache.cs | 10 ++++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Recyclarr.Cli/Cache/CachePersister.cs b/src/Recyclarr.Cli/Cache/CachePersister.cs index 4bda7c67..59979c98 100644 --- a/src/Recyclarr.Cli/Cache/CachePersister.cs +++ b/src/Recyclarr.Cli/Cache/CachePersister.cs @@ -1,3 +1,4 @@ +using Newtonsoft.Json; using Recyclarr.TrashLib.Config.Services; namespace Recyclarr.Cli.Cache; @@ -31,14 +32,12 @@ public class CachePersister : ICachePersister throw new CacheException("Version mismatch"); } - _log.Debug("Loaded Cache"); return cache; } public void Save(IServiceConfiguration config, CustomFormatCache cache) { - _log.Debug("Saving Cache with {Mappings}", cache.TrashIdMappings - .Select(x => $"TrashID: {x.TrashId}, Name: {x.CustomFormatName}, FormatID: {x.CustomFormatId}")); + _log.Debug("Saving Cache with {Mappings}", JsonConvert.SerializeObject(cache.TrashIdMappings)); _cache.Save(cache, config); } } diff --git a/src/Recyclarr.Cli/Cache/ServiceCache.cs b/src/Recyclarr.Cli/Cache/ServiceCache.cs index 87697d30..af70113b 100644 --- a/src/Recyclarr.Cli/Cache/ServiceCache.cs +++ b/src/Recyclarr.Cli/Cache/ServiceCache.cs @@ -13,11 +13,12 @@ public partial class ServiceCache : IServiceCache { private readonly ICacheStoragePath _storagePath; private readonly JsonSerializerSettings _jsonSettings; + private readonly ILogger _log; public ServiceCache(ICacheStoragePath storagePath, ILogger log) { _storagePath = storagePath; - Log = log; + _log = log; _jsonSettings = new JsonSerializerSettings { Formatting = Formatting.Indented, @@ -28,13 +29,13 @@ public partial class ServiceCache : IServiceCache }; } - private ILogger Log { get; } - public T? Load(IServiceConfiguration config) where T : class { var path = PathFromAttribute(config); + _log.Debug("Loading cache from path: {Path}", path.FullName); if (!path.Exists) { + _log.Debug("Cache path does not exist"); return null; } @@ -47,7 +48,7 @@ public partial class ServiceCache : IServiceCache } catch (JsonException e) { - Log.Error("Failed to read cache data, will proceed without cache. Reason: {Msg}", e.Message); + _log.Error("Failed to read cache data, will proceed without cache. Reason: {Msg}", e.Message); } return null; @@ -56,6 +57,7 @@ public partial class ServiceCache : IServiceCache public void Save(T obj, IServiceConfiguration config) where T : class { var path = PathFromAttribute(config); + _log.Debug("Saving cache to path: {Path}", path.FullName); path.CreateParentDirectory(); var serializer = JsonSerializer.Create(_jsonSettings);