Use Serilog in EntityFramework Core logging.

This commit is contained in:
2025-07-24 21:23:53 +01:00
parent 1435aa10ee
commit bf19439e49
15 changed files with 69 additions and 36 deletions

View File

@@ -7,6 +7,7 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using RomRepoMgr.Database;
using RomRepoMgr.Database.Models;
using SharpCompress.Compressors;
@@ -18,7 +19,7 @@ namespace RomRepoMgr.Core.Filesystem;
// TODO: Invalidate caches
// TODO: Mount options
// TODO: Do not show machines or romsets with no ROMs in repo
public class Vfs : IDisposable
public class Vfs(ILoggerFactory loggerFactory) : IDisposable
{
readonly ConcurrentDictionary<ulong, ConcurrentDictionary<string, CachedDisk>> _machineDisksCache = [];
readonly ConcurrentDictionary<ulong, ConcurrentDictionary<string, CachedFile>> _machineFilesCache = [];
@@ -98,7 +99,7 @@ public class Vfs : IDisposable
internal void GetInfo(out ulong files, out ulong totalSize)
{
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath, loggerFactory);
totalSize = (ulong)(ctx.Files.Where(f => f.IsInRepo).Sum(f => (double)f.Size) +
ctx.Disks.Where(f => f.IsInRepo).Sum(f => (double)f.Size) +
@@ -114,7 +115,7 @@ public class Vfs : IDisposable
void FillRootDirectoryCache()
{
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath, loggerFactory);
var rootCache = new ConcurrentDictionary<string, long>();
@@ -180,7 +181,7 @@ public class Vfs : IDisposable
{
if(_romSetsCache.TryGetValue(id, out RomSet romSet)) return romSet;
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath, loggerFactory);
romSet = ctx.RomSets.Find(id);
@@ -199,7 +200,7 @@ public class Vfs : IDisposable
cachedMachines = [];
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath, loggerFactory);
foreach(Machine mach in ctx.Machines.Where(m => m.RomSet.Id == id))
{
@@ -231,7 +232,7 @@ public class Vfs : IDisposable
if(cachedMachineFiles != null) return cachedMachineFiles;
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath, loggerFactory);
cachedMachineFiles = [];
@@ -267,7 +268,7 @@ public class Vfs : IDisposable
if(cachedMachineDisks != null) return cachedMachineDisks;
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath, loggerFactory);
cachedMachineDisks = [];
@@ -301,7 +302,7 @@ public class Vfs : IDisposable
if(cachedMachineMedias != null) return cachedMachineMedias;
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath, loggerFactory);
cachedMachineMedias = [];

View File

@@ -33,6 +33,7 @@ using System.Linq;
using System.Threading;
using EFCore.BulkExtensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using RomRepoMgr.Core.Checksums;
using RomRepoMgr.Core.EventArgs;
using RomRepoMgr.Core.Models;
@@ -54,16 +55,18 @@ namespace RomRepoMgr.Core.Workers;
public sealed class DatImporter
{
static readonly Lock DbLock = new();
readonly string _category;
readonly string _datFilesPath;
readonly string _datPath;
bool _aborted;
static readonly Lock DbLock = new();
readonly string _category;
readonly string _datFilesPath;
readonly string _datPath;
readonly ILoggerFactory _loggerFactory;
bool _aborted;
public DatImporter(string datPath, string category)
public DatImporter(string datPath, string category, ILoggerFactory loggerFactory)
{
_datPath = datPath;
_datFilesPath = Path.Combine(Settings.Settings.Current.RepositoryPath, "datfiles");
_datPath = datPath;
_datFilesPath = Path.Combine(Settings.Settings.Current.RepositoryPath, "datfiles");
_loggerFactory = loggerFactory;
if(!string.IsNullOrWhiteSpace(category)) _category = category;
}
@@ -72,7 +75,7 @@ public sealed class DatImporter
{
try
{
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath, _loggerFactory);
SetIndeterminateProgress?.Invoke(this, System.EventArgs.Empty);

View File

@@ -6,6 +6,7 @@ using System.Text;
using System.Threading.Tasks;
using Ionic.Zip;
using Ionic.Zlib;
using Microsoft.Extensions.Logging;
using RomRepoMgr.Core.EventArgs;
using RomRepoMgr.Core.Resources;
using RomRepoMgr.Database;
@@ -15,7 +16,7 @@ using CompressionMode = SharpCompress.Compressors.CompressionMode;
namespace RomRepoMgr.Core.Workers;
public class FileExporter(long romSetId, string outPath)
public class FileExporter(long romSetId, string outPath, ILoggerFactory loggerFactory)
{
const long BUFFER_SIZE = 131072;
long _filePosition;
@@ -43,7 +44,7 @@ public class FileExporter(long romSetId, string outPath)
Message = Localization.RetrievingRomSetFromDatabase
});
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath, loggerFactory);
RomSet romSet = ctx.RomSets.Find(romSetId);
@@ -108,7 +109,7 @@ public class FileExporter(long romSetId, string outPath)
Message = machine.Name
});
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath, loggerFactory);
string machineName = machine.Name;