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;

View File

@@ -41,13 +41,13 @@ public sealed class Context(DbContextOptions options) : DbContext(options)
public DbSet<MediaByMachine> MediasByMachines { get; set; }
public DbSet<RomSetStat> RomSetStats { get; set; }
public static Context Create(string dbPath)
public static Context Create(string dbPath, ILoggerFactory loggerFactory)
{
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseLazyLoadingProxies()
#if DEBUG
.UseLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole()))
.UseLoggerFactory(loggerFactory)
#endif
.UseSqlite($"Data Source={dbPath}");

View File

@@ -24,10 +24,12 @@
*******************************************************************************/
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Logging;
namespace RomRepoMgr.Database;
public class ContextFactory : IDesignTimeDbContextFactory<Context>
{
public Context CreateDbContext(string[] args) => Context.Create("romrepo.db");
public Context CreateDbContext(string[] args) =>
Context.Create("romrepo.db", LoggerFactory.Create(builder => builder.AddConsole()));
}

View File

@@ -33,6 +33,8 @@ using RomRepoMgr.Core.Models;
using RomRepoMgr.Database;
using RomRepoMgr.Database.Models;
using RomRepoMgr.Views;
using Serilog;
using Serilog.Extensions.Logging;
namespace RomRepoMgr.ViewModels;
@@ -207,7 +209,8 @@ public partial class EditDatViewModel : ViewModelBase
async Task ExecuteSaveCommandAsync()
{
await using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
await using var ctx =
Context.Create(Settings.Settings.Current.DatabasePath, new SerilogLoggerFactory(Log.Logger));
RomSet romSetDb = await ctx.RomSets.FindAsync(_romSet.Id);

View File

@@ -32,6 +32,8 @@ using CommunityToolkit.Mvvm.Input;
using RomRepoMgr.Core.EventArgs;
using RomRepoMgr.Core.Workers;
using RomRepoMgr.Views;
using Serilog;
using Serilog.Extensions.Logging;
namespace RomRepoMgr.ViewModels;
@@ -154,7 +156,7 @@ public sealed partial class ExportRomsViewModel : ViewModelBase
public void OnOpened()
{
var worker = new FileExporter(_romSetId, FolderPath);
var worker = new FileExporter(_romSetId, FolderPath, new SerilogLoggerFactory(Log.Logger));
worker.SetMessage += OnWorkerOnSetMessage;
worker.SetProgress += OnWorkerOnSetProgress;
worker.SetProgressBounds += OnWorkerOnSetProgressBounds;

View File

@@ -14,6 +14,8 @@ using CommunityToolkit.Mvvm.Input;
using RomRepoMgr.Core.EventArgs;
using RomRepoMgr.Models;
using RomRepoMgr.Resources;
using Serilog;
using Serilog.Extensions.Logging;
namespace RomRepoMgr.ViewModels;
@@ -134,7 +136,9 @@ public sealed partial class ImportDatFolderViewModel : ViewModelBase
Indeterminate = false
};
var worker = new Core.Workers.DatImporter(_datFiles[_listPosition], Category);
var worker =
new Core.Workers.DatImporter(_datFiles[_listPosition], Category, new SerilogLoggerFactory(Log.Logger));
worker.ErrorOccurred += model.OnErrorOccurred;
worker.SetIndeterminateProgress += model.OnSetIndeterminateProgress;
worker.SetMessage += model.OnSetMessage;

View File

@@ -32,6 +32,8 @@ using CommunityToolkit.Mvvm.Input;
using RomRepoMgr.Core.EventArgs;
using RomRepoMgr.Core.Workers;
using RomRepoMgr.Views;
using Serilog;
using Serilog.Extensions.Logging;
namespace RomRepoMgr.ViewModels;
@@ -68,7 +70,7 @@ public sealed partial class ImportDatViewModel : ViewModelBase
IndeterminateProgress = true;
ProgressVisible = false;
ErrorVisible = false;
_worker = new DatImporter(datPath, null);
_worker = new DatImporter(datPath, null, new SerilogLoggerFactory(Log.Logger));
_worker.ErrorOccurred += OnWorkerOnErrorOccurred;
_worker.SetIndeterminateProgress += OnWorkerOnSetIndeterminateProgress;
_worker.SetMessage += OnWorkerOnSetMessage;

View File

@@ -19,12 +19,14 @@ using RomRepoMgr.Database.Models;
using RomRepoMgr.Models;
using RomRepoMgr.Resources;
using Serilog;
using Serilog.Extensions.Logging;
namespace RomRepoMgr.ViewModels;
public sealed partial class ImportRomFolderViewModel : ViewModelBase
{
readonly Context _ctx = Context.Create(Settings.Settings.Current.DatabasePath);
readonly Context _ctx =
Context.Create(Settings.Settings.Current.DatabasePath, new SerilogLoggerFactory(Log.Logger));
readonly ConcurrentBag<DbDisk> _newDisks = [];
readonly ConcurrentBag<DbFile> _newFiles = [];
readonly ConcurrentBag<DbMedia> _newMedias = [];

View File

@@ -45,6 +45,7 @@ using RomRepoMgr.Core.Models;
using RomRepoMgr.Resources;
using RomRepoMgr.Views;
using Serilog;
using Serilog.Extensions.Logging;
namespace RomRepoMgr.ViewModels;
@@ -267,7 +268,7 @@ public sealed partial class MainWindowViewModel : ViewModelBase
try
{
Vfs = new Vfs();
Vfs = new Vfs(new SerilogLoggerFactory(Log.Logger));
Vfs.Umounted += VfsOnUmounted;
Vfs.MountTo(result[0].Path.LocalPath);
}

View File

@@ -32,6 +32,8 @@ using RomRepoMgr.Database;
using RomRepoMgr.Database.Models;
using RomRepoMgr.Resources;
using RomRepoMgr.Views;
using Serilog;
using Serilog.Extensions.Logging;
namespace RomRepoMgr.ViewModels;
@@ -55,7 +57,8 @@ public sealed partial class RemoveDatViewModel : ViewModelBase
{
_ = Task.Run(() =>
{
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath,
new SerilogLoggerFactory(Log.Logger));
Dispatcher.UIThread.Post(() => StatusMessage = Localization.RetrievingRomSetFromDatabase);

View File

@@ -40,6 +40,8 @@ using RomRepoMgr.Core.Workers;
using RomRepoMgr.Database;
using RomRepoMgr.Resources;
using RomRepoMgr.Views;
using Serilog;
using Serilog.Extensions.Logging;
using ErrorEventArgs = RomRepoMgr.Core.EventArgs.ErrorEventArgs;
namespace RomRepoMgr.ViewModels;
@@ -227,7 +229,7 @@ public sealed partial class SettingsViewModel : ViewModelBase
{
try
{
var ctx = Context.Create(result);
var ctx = Context.Create(result, new SerilogLoggerFactory(Log.Logger));
await ctx.Database.MigrateAsync();
}
catch
@@ -295,7 +297,7 @@ public sealed partial class SettingsViewModel : ViewModelBase
try
{
var ctx = Context.Create(result);
var ctx = Context.Create(result, new SerilogLoggerFactory(Log.Logger));
await ctx.Database.MigrateAsync();
}
catch

View File

@@ -39,6 +39,7 @@ using RomRepoMgr.Core.Models;
using RomRepoMgr.Core.Workers;
using RomRepoMgr.Database;
using Serilog;
using Serilog.Extensions.Logging;
namespace RomRepoMgr.ViewModels;
@@ -175,7 +176,8 @@ public sealed partial class SplashWindowViewModel : ViewModelBase
if(!Directory.Exists(dbPathFolder)) Directory.CreateDirectory(dbPathFolder);
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath,
new SerilogLoggerFactory(Log.Logger));
Dispatcher.UIThread.Post(MigrateDatabase);
}
@@ -203,7 +205,8 @@ public sealed partial class SplashWindowViewModel : ViewModelBase
{
try
{
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath,
new SerilogLoggerFactory(Log.Logger));
ctx.Database.Migrate();
@@ -233,7 +236,8 @@ public sealed partial class SplashWindowViewModel : ViewModelBase
{
try
{
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath,
new SerilogLoggerFactory(Log.Logger));
GotRomSets?.Invoke(this,
new RomSetsEventArgs

View File

@@ -36,6 +36,8 @@ using RomRepoMgr.Database;
using RomRepoMgr.Database.Models;
using RomRepoMgr.Resources;
using RomRepoMgr.Views;
using Serilog;
using Serilog.Extensions.Logging;
namespace RomRepoMgr.ViewModels;
@@ -79,7 +81,8 @@ public sealed partial class UpdateStatsViewModel : ViewModelBase
{
_ = Task.Run(() =>
{
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath,
new SerilogLoggerFactory(Log.Logger));
Dispatcher.UIThread.Post(() =>
{