refactor: Improve logging for caching logic

pull/201/head
Robert Dailey 11 months ago
parent 67d604d8a0
commit 9bdb5f364c

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

@ -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<T>(IServiceConfiguration config) where T : class
{
var path = PathFromAttribute<T>(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>(T obj, IServiceConfiguration config) where T : class
{
var path = PathFromAttribute<T>(config);
_log.Debug("Saving cache to path: {Path}", path.FullName);
path.CreateParentDirectory();
var serializer = JsonSerializer.Create(_jsonSettings);

Loading…
Cancel
Save