diff --git a/RomRepoMgr.Core/Filesystem/Vfs.cs b/RomRepoMgr.Core/Filesystem/Vfs.cs index d3eae22..31840fa 100644 --- a/RomRepoMgr.Core/Filesystem/Vfs.cs +++ b/RomRepoMgr.Core/Filesystem/Vfs.cs @@ -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> _machineDisksCache = []; readonly ConcurrentDictionary> _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(); @@ -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 = []; diff --git a/RomRepoMgr.Core/Workers/DatImporter.cs b/RomRepoMgr.Core/Workers/DatImporter.cs index 131b149..cfde823 100644 --- a/RomRepoMgr.Core/Workers/DatImporter.cs +++ b/RomRepoMgr.Core/Workers/DatImporter.cs @@ -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); diff --git a/RomRepoMgr.Core/Workers/FileExporter.cs b/RomRepoMgr.Core/Workers/FileExporter.cs index 0a1a316..de3ba3d 100644 --- a/RomRepoMgr.Core/Workers/FileExporter.cs +++ b/RomRepoMgr.Core/Workers/FileExporter.cs @@ -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; diff --git a/RomRepoMgr.Database/Context.cs b/RomRepoMgr.Database/Context.cs index 2f926cf..a7c9a57 100644 --- a/RomRepoMgr.Database/Context.cs +++ b/RomRepoMgr.Database/Context.cs @@ -41,13 +41,13 @@ public sealed class Context(DbContextOptions options) : DbContext(options) public DbSet MediasByMachines { get; set; } public DbSet 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}"); diff --git a/RomRepoMgr.Database/ContextFactory.cs b/RomRepoMgr.Database/ContextFactory.cs index 7333159..942e2e9 100644 --- a/RomRepoMgr.Database/ContextFactory.cs +++ b/RomRepoMgr.Database/ContextFactory.cs @@ -24,10 +24,12 @@ *******************************************************************************/ using Microsoft.EntityFrameworkCore.Design; +using Microsoft.Extensions.Logging; namespace RomRepoMgr.Database; public class ContextFactory : IDesignTimeDbContextFactory { - public Context CreateDbContext(string[] args) => Context.Create("romrepo.db"); + public Context CreateDbContext(string[] args) => + Context.Create("romrepo.db", LoggerFactory.Create(builder => builder.AddConsole())); } \ No newline at end of file diff --git a/RomRepoMgr/ViewModels/EditDatViewModel.cs b/RomRepoMgr/ViewModels/EditDatViewModel.cs index ae650d6..fba89ec 100644 --- a/RomRepoMgr/ViewModels/EditDatViewModel.cs +++ b/RomRepoMgr/ViewModels/EditDatViewModel.cs @@ -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); diff --git a/RomRepoMgr/ViewModels/ExportRomsViewModel.cs b/RomRepoMgr/ViewModels/ExportRomsViewModel.cs index 250a876..9f3a379 100644 --- a/RomRepoMgr/ViewModels/ExportRomsViewModel.cs +++ b/RomRepoMgr/ViewModels/ExportRomsViewModel.cs @@ -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; diff --git a/RomRepoMgr/ViewModels/ImportDatFolderViewModel.cs b/RomRepoMgr/ViewModels/ImportDatFolderViewModel.cs index b72bfb7..38cb833 100644 --- a/RomRepoMgr/ViewModels/ImportDatFolderViewModel.cs +++ b/RomRepoMgr/ViewModels/ImportDatFolderViewModel.cs @@ -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; diff --git a/RomRepoMgr/ViewModels/ImportDatViewModel.cs b/RomRepoMgr/ViewModels/ImportDatViewModel.cs index 093ce22..51ef6d1 100644 --- a/RomRepoMgr/ViewModels/ImportDatViewModel.cs +++ b/RomRepoMgr/ViewModels/ImportDatViewModel.cs @@ -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; diff --git a/RomRepoMgr/ViewModels/ImportRomFolderViewModel.cs b/RomRepoMgr/ViewModels/ImportRomFolderViewModel.cs index d03e5c6..70084e5 100644 --- a/RomRepoMgr/ViewModels/ImportRomFolderViewModel.cs +++ b/RomRepoMgr/ViewModels/ImportRomFolderViewModel.cs @@ -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 _newDisks = []; readonly ConcurrentBag _newFiles = []; readonly ConcurrentBag _newMedias = []; diff --git a/RomRepoMgr/ViewModels/MainWindowViewModel.cs b/RomRepoMgr/ViewModels/MainWindowViewModel.cs index 840272a..bfbcc3f 100644 --- a/RomRepoMgr/ViewModels/MainWindowViewModel.cs +++ b/RomRepoMgr/ViewModels/MainWindowViewModel.cs @@ -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); } diff --git a/RomRepoMgr/ViewModels/RemoveDatViewModel.cs b/RomRepoMgr/ViewModels/RemoveDatViewModel.cs index b6e00cb..d299299 100644 --- a/RomRepoMgr/ViewModels/RemoveDatViewModel.cs +++ b/RomRepoMgr/ViewModels/RemoveDatViewModel.cs @@ -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); diff --git a/RomRepoMgr/ViewModels/SettingsViewModel.cs b/RomRepoMgr/ViewModels/SettingsViewModel.cs index d6c5d34..1908188 100644 --- a/RomRepoMgr/ViewModels/SettingsViewModel.cs +++ b/RomRepoMgr/ViewModels/SettingsViewModel.cs @@ -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 diff --git a/RomRepoMgr/ViewModels/SplashWindowViewModel.cs b/RomRepoMgr/ViewModels/SplashWindowViewModel.cs index 7290e31..59d4b57 100644 --- a/RomRepoMgr/ViewModels/SplashWindowViewModel.cs +++ b/RomRepoMgr/ViewModels/SplashWindowViewModel.cs @@ -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 diff --git a/RomRepoMgr/ViewModels/UpdateStatsViewModel.cs b/RomRepoMgr/ViewModels/UpdateStatsViewModel.cs index c9d71c6..d15cfab 100644 --- a/RomRepoMgr/ViewModels/UpdateStatsViewModel.cs +++ b/RomRepoMgr/ViewModels/UpdateStatsViewModel.cs @@ -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(() => {