From 80894fe4468e61d039c3529b747b21658f7f9c0a Mon Sep 17 00:00:00 2001 From: rootdarkarchon Date: Thu, 15 Feb 2024 12:30:28 +0100 Subject: [PATCH] logging adjustments etc --- MareSynchronos/FileCache/FileCacheManager.cs | 27 ++++++++++---------- MareSynchronos/Services/CharacterAnalyzer.cs | 4 +-- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/MareSynchronos/FileCache/FileCacheManager.cs b/MareSynchronos/FileCache/FileCacheManager.cs index d3602bf..e32c150 100644 --- a/MareSynchronos/FileCache/FileCacheManager.cs +++ b/MareSynchronos/FileCache/FileCacheManager.cs @@ -60,15 +60,19 @@ public sealed class FileCacheManager : IHostedService public List GetAllFileCaches() => _fileCaches.Values.SelectMany(v => v).ToList(); - public List GetAllFileCachesByHash(string hash) + public List GetAllFileCachesByHash(string hash, bool ignoreCacheEntries = false, bool validate = true) { List output = []; if (_fileCaches.TryGetValue(hash, out var fileCacheEntities)) { - foreach (var fileCache in fileCacheEntities.ToList()) + foreach (var fileCache in fileCacheEntities.Where(c => ignoreCacheEntries ? !c.IsCacheEntry : true).ToList()) { - var validated = GetValidatedFileCache(fileCache); - if (validated != null) output.Add(validated); + if (!validate) output.Add(fileCache); + else + { + var validated = GetValidatedFileCache(fileCache); + if (validated != null) output.Add(validated); + } } } @@ -188,11 +192,6 @@ public sealed class FileCacheManager : IHostedService .Replace("\\\\", "\\", StringComparison.Ordinal), StringComparer.OrdinalIgnoreCase); - foreach (var entry in cleanedPaths) - { - _logger.LogInformation("Getting FileCache by Cleaned Path: {path}", entry.Value); - } - Dictionary result = new(StringComparer.OrdinalIgnoreCase); var dict = _fileCaches.SelectMany(f => f.Value) @@ -200,7 +199,7 @@ public sealed class FileCacheManager : IHostedService foreach (var entry in cleanedPaths) { - _logger.LogDebug("Validating {path}", entry.Value); + _logger.LogDebug("Checking {path}", entry.Value); if (dict.TryGetValue(entry.Value, out var entity)) { @@ -229,7 +228,7 @@ public sealed class FileCacheManager : IHostedService if (_fileCaches.TryGetValue(hash, out var caches)) { var removedCount = caches?.RemoveAll(c => string.Equals(c.PrefixedFilePath, prefixedFilePath, StringComparison.Ordinal)); - _logger.LogDebug("Removed {count} files with hash {hash} and file cache {path}", removedCount, hash, prefixedFilePath); + _logger.LogTrace("Removed from DB: {count} file(s) with hash {hash} and file cache {path}", removedCount, hash, prefixedFilePath); if (caches?.Count == 0) { @@ -308,7 +307,7 @@ public sealed class FileCacheManager : IHostedService var newHashedEntity = new FileCacheEntity(fileCache.Hash, fileCache.PrefixedFilePath + "." + ext, DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture)); newHashedEntity.SetResolvedFilePath(extensionPath); AddHashedFile(newHashedEntity); - _logger.LogDebug("Migrated from {oldPath} to {newPath}", fileCache.ResolvedFilepath, newHashedEntity.ResolvedFilepath); + _logger.LogTrace("Migrated from {oldPath} to {newPath}", fileCache.ResolvedFilepath, newHashedEntity.ResolvedFilepath); return newHashedEntity; } catch (Exception ex) @@ -328,6 +327,7 @@ public sealed class FileCacheManager : IHostedService if (!entries.Exists(u => string.Equals(u.PrefixedFilePath, fileCache.PrefixedFilePath, StringComparison.OrdinalIgnoreCase))) { + _logger.LogTrace("Adding to DB: {hash} => {path}", fileCache.Hash, fileCache.PrefixedFilePath); entries.Add(fileCache); } } @@ -343,13 +343,14 @@ public sealed class FileCacheManager : IHostedService File.AppendAllLines(_csvPath, new[] { entity.CsvEntry }); } var result = GetFileCacheByPath(fileInfo.FullName); - _logger.LogDebug("Creating cache entity for {name} success: {success}", fileInfo.FullName, (result != null)); + _logger.LogTrace("Creating cache entity for {name} success: {success}", fileInfo.FullName, (result != null)); return result; } private FileCacheEntity? GetValidatedFileCache(FileCacheEntity fileCache) { var resultingFileCache = ReplacePathPrefixes(fileCache); + _logger.LogTrace("Validating {path}", fileCache.PrefixedFilePath); resultingFileCache = Validate(resultingFileCache); return resultingFileCache; } diff --git a/MareSynchronos/Services/CharacterAnalyzer.cs b/MareSynchronos/Services/CharacterAnalyzer.cs index ff519bc..ab6e5ca 100644 --- a/MareSynchronos/Services/CharacterAnalyzer.cs +++ b/MareSynchronos/Services/CharacterAnalyzer.cs @@ -98,7 +98,7 @@ public sealed class CharacterAnalyzer : MediatorSubscriberBase, IDisposable Dictionary data = new(StringComparer.OrdinalIgnoreCase); foreach (var fileEntry in obj.Value) { - var fileCacheEntries = _fileCacheManager.GetAllFileCachesByHash(fileEntry.Hash).Where(c => !c.IsCacheEntry).ToList(); + var fileCacheEntries = _fileCacheManager.GetAllFileCachesByHash(fileEntry.Hash, ignoreCacheEntries: true, validate: false).ToList(); if (fileCacheEntries.Count == 0) continue; var filePath = fileCacheEntries[0].ResolvedFilepath; @@ -183,7 +183,7 @@ public sealed class CharacterAnalyzer : MediatorSubscriberBase, IDisposable { var compressedsize = await fileCacheManager.GetCompressedFileLength(Hash, token).ConfigureAwait(false); var normalSize = new FileInfo(FilePaths[0]).Length; - var entries = fileCacheManager.GetAllFileCachesByHash(Hash); + var entries = fileCacheManager.GetAllFileCachesByHash(Hash, ignoreCacheEntries: true, validate: false); foreach (var entry in entries) { entry.Size = normalSize;