diff --git a/PiwigoDirectorySync/Commands/DownloadImagesCommand.cs b/PiwigoDirectorySync/Commands/DownloadImagesCommand.cs index 9ab9b04..4d00615 100644 --- a/PiwigoDirectorySync/Commands/DownloadImagesCommand.cs +++ b/PiwigoDirectorySync/Commands/DownloadImagesCommand.cs @@ -1,7 +1,7 @@ using System.Diagnostics; -using Microsoft.Extensions.Logging; using PiwigoDirectorySync.Infrastructure; using PiwigoDirectorySync.Services; +using Serilog; using Spectre.Console.Cli; namespace PiwigoDirectorySync.Commands; @@ -9,23 +9,22 @@ namespace PiwigoDirectorySync.Commands; internal class DownloadImagesCommand : CancellableAsyncCommand { private readonly IImageSynchronizer _imageSynchronizer; - private readonly ILogger _logger; + private readonly ILogger _logger = Log.ForContext(); - public DownloadImagesCommand(IImageSynchronizer imageSynchronizer, ILogger logger) + public DownloadImagesCommand(IImageSynchronizer imageSynchronizer) { _imageSynchronizer = imageSynchronizer; - _logger = logger; } protected override async Task ExecuteAsync(CommandContext context, DownloadImagesSettings settings, CancellationToken cancellationToken) { - _logger.LogInformation("Starting image download for piwigo server {SettingsPiwigoServerId}", settings.PiwigoServerId); + _logger.Information("Starting image download for piwigo server {SettingsPiwigoServerId}", settings.PiwigoServerId); var stopWatch = Stopwatch.StartNew(); await _imageSynchronizer.DownloadImagesAsync(settings.PiwigoServerId, cancellationToken); stopWatch.Stop(); - _logger.LogInformation("Synchronized all images with piwigo server {SettingsPiwigoServerId} in {ElapsedTotalSeconds} seconds", settings.PiwigoServerId, + _logger.Information("Synchronized all images with piwigo server {SettingsPiwigoServerId} in {ElapsedTotalSeconds} seconds", settings.PiwigoServerId, stopWatch.Elapsed.TotalSeconds); return 0; diff --git a/PiwigoDirectorySync/Commands/PiwigoAddCommand.cs b/PiwigoDirectorySync/Commands/PiwigoAddCommand.cs index 0f59100..b4f7ffd 100644 --- a/PiwigoDirectorySync/Commands/PiwigoAddCommand.cs +++ b/PiwigoDirectorySync/Commands/PiwigoAddCommand.cs @@ -1,7 +1,7 @@ using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging; using PiwigoDirectorySync.Infrastructure; using PiwigoDirectorySync.Persistence; +using Serilog; using Spectre.Console; using Spectre.Console.Cli; @@ -9,24 +9,22 @@ namespace PiwigoDirectorySync.Commands; internal class PiwigoAddCommand : CancellableAsyncCommand { - private readonly ILogger _logger; + private readonly ILogger _logger = Log.ForContext(); private readonly PersistenceContext _persistenceContext; - public PiwigoAddCommand(ILogger logger, PersistenceContext persistenceContext) + public PiwigoAddCommand(PersistenceContext persistenceContext) { - _logger = logger ?? throw new ArgumentNullException(nameof(logger)); - _persistenceContext = persistenceContext ?? throw new ArgumentNullException(nameof(persistenceContext)); + _persistenceContext = persistenceContext; } protected override async Task ExecuteAsync(CommandContext context, PiwigoAddSettings settings, CancellationToken cancellation) { - _logger.LogInformation("Adding piwigo server {SettingsServerName} with user {SettingsUserName} and url {SettingsUrl}", settings.ServerName, settings.UserName, - settings.Url); + _logger.Information("Adding piwigo server {SettingsServerName} with user {SettingsUserName} and url {SettingsUrl}", settings.ServerName, settings.UserName, settings.Url); var count = await _persistenceContext.PiwigoServers.Where(s => s.Name == settings.ServerName).CountAsync(cancellation); if (count > 0) { - _logger.LogWarning("Server with name {SettingsServerName} already exists. Not updated", settings.ServerName); + _logger.Warning("Server with name {SettingsServerName} already exists. Not updated", settings.ServerName); AnsiConsole.MarkupLine($"[maroon]There is already a server with the name {settings.ServerName}[/]"); return -1; } diff --git a/PiwigoDirectorySync/Commands/PiwigoListCommand.cs b/PiwigoDirectorySync/Commands/PiwigoListCommand.cs index 3a29fce..2b0dcce 100644 --- a/PiwigoDirectorySync/Commands/PiwigoListCommand.cs +++ b/PiwigoDirectorySync/Commands/PiwigoListCommand.cs @@ -1,5 +1,4 @@ using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging; using PiwigoDirectorySync.Infrastructure; using PiwigoDirectorySync.Persistence; using Spectre.Console; @@ -9,13 +8,11 @@ namespace PiwigoDirectorySync.Commands; internal class PiwigoListCommand : CancellableAsyncCommand { - private readonly ILogger _logger; private readonly PersistenceContext _persistenceContext; - public PiwigoListCommand(ILogger logger, PersistenceContext persistenceContext) + public PiwigoListCommand(PersistenceContext persistenceContext) { - _logger = logger ?? throw new ArgumentNullException(nameof(logger)); - _persistenceContext = persistenceContext ?? throw new ArgumentNullException(nameof(persistenceContext)); + _persistenceContext = persistenceContext; } protected override async Task ExecuteAsync(CommandContext context, PiwigoListSettings settings, CancellationToken cancellation) diff --git a/PiwigoDirectorySync/Commands/PiwigoRemoveCommand.cs b/PiwigoDirectorySync/Commands/PiwigoRemoveCommand.cs index 208dd76..f92f27c 100644 --- a/PiwigoDirectorySync/Commands/PiwigoRemoveCommand.cs +++ b/PiwigoDirectorySync/Commands/PiwigoRemoveCommand.cs @@ -1,7 +1,7 @@ using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging; using PiwigoDirectorySync.Infrastructure; using PiwigoDirectorySync.Persistence; +using Serilog; using Spectre.Console; using Spectre.Console.Cli; @@ -9,12 +9,11 @@ namespace PiwigoDirectorySync.Commands; internal class PiwigoRemoveCommand : CancellableAsyncCommand { - private readonly ILogger _logger; + private readonly ILogger _logger = Log.ForContext(); private readonly PersistenceContext _persistenceContext; - public PiwigoRemoveCommand(ILogger logger, PersistenceContext persistenceContext) + public PiwigoRemoveCommand(PersistenceContext persistenceContext) { - _logger = logger; _persistenceContext = persistenceContext; } @@ -23,29 +22,44 @@ internal class PiwigoRemoveCommand : CancellableAsyncCommand i.Album.ServerId == server.Id).ExecuteDeleteAsync(cancellation); - await _persistenceContext.SaveChangesAsync(cancellation); + _logger.Information("Removing {ServerName} including images and albums from database", serverName); + + await AnsiConsole.Status() + .Spinner(Spinner.Known.Star) + .SpinnerStyle(Style.Parse("green bold")) + .StartAsync("[orange3]Updating database[/]", async ctx => + { + ctx.Status($"[orange3]Removing image metadata of server {serverName}![/]"); + var deletedImages = await _persistenceContext.PiwigoImages.Where(i => i.Album.ServerId == serverId).ExecuteDeleteAsync(cancellation); + await _persistenceContext.SaveChangesAsync(cancellation); + ctx.Status($"[green]Deleted {deletedImages} images from database[/]"); - AnsiConsole.MarkupLine($"[orange3]Removing album metadata of server {server.Name}![/]"); - await _persistenceContext.PiwigoAlbums.Where(a => a.ServerId == server.Id).ExecuteDeleteAsync(cancellation); - await _persistenceContext.SaveChangesAsync(cancellation); + ctx.Status($"[orange3]Removing album metadata of server {serverName}![/]"); + var deletedAlbums = await _persistenceContext.PiwigoAlbums.Where(a => a.ServerId == serverId).ExecuteDeleteAsync(cancellation); + await _persistenceContext.SaveChangesAsync(cancellation); + ctx.Status($"[green]Deleted {deletedAlbums} albums from database[/]"); - AnsiConsole.MarkupLine($"[orange3]Removing server {server.Name}![/]"); - await _persistenceContext.PiwigoServers.Where(s => s.Id == server.Id).ExecuteDeleteAsync(cancellation); - await _persistenceContext.SaveChangesAsync(cancellation); + ctx.Status($"[orange3]Removing server {serverName}![/]"); + await _persistenceContext.PiwigoServers.Where(s => s.Id == serverId).ExecuteDeleteAsync(cancellation); + await _persistenceContext.SaveChangesAsync(cancellation); + ctx.Status($"[green]Deleted piwigo server {serverName}[/]"); + }); + + _logger.Information("Removed {ServerName} including images and albums from database", serverName); return 0; } diff --git a/PiwigoDirectorySync/Commands/PiwigoUpdateCommand.cs b/PiwigoDirectorySync/Commands/PiwigoUpdateCommand.cs index 2bcd672..eef26df 100644 --- a/PiwigoDirectorySync/Commands/PiwigoUpdateCommand.cs +++ b/PiwigoDirectorySync/Commands/PiwigoUpdateCommand.cs @@ -1,8 +1,8 @@ using System.Diagnostics.CodeAnalysis; using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging; using PiwigoDirectorySync.Infrastructure; using PiwigoDirectorySync.Persistence; +using Serilog; using Spectre.Console; using Spectre.Console.Cli; @@ -10,24 +10,23 @@ namespace PiwigoDirectorySync.Commands; internal class PiwigoUpdateCommand : CancellableAsyncCommand { - private readonly ILogger _logger; + private readonly ILogger _logger = Log.ForContext(); + private readonly PersistenceContext _persistenceContext; - public PiwigoUpdateCommand(ILogger logger, PersistenceContext persistenceContext) + public PiwigoUpdateCommand(PersistenceContext persistenceContext) { - _logger = logger ?? throw new ArgumentNullException(nameof(logger)); - _persistenceContext = persistenceContext ?? throw new ArgumentNullException(nameof(persistenceContext)); + _persistenceContext = persistenceContext; } protected override async Task ExecuteAsync(CommandContext context, PiwigoUpdateSettings settings, CancellationToken cancellation) { - _logger.LogInformation("Updating piwigo server {SettingsServerName} with user {SettingsUserName} and url {SettingsUrl}", settings.ServerName, settings.UserName, - settings.Url); + _logger.Information("Updating piwigo server {SettingsServerName} with user {SettingsUserName} and url {SettingsUrl}", settings.ServerName, settings.UserName, settings.Url); var count = await _persistenceContext.PiwigoServers.Where(s => s.Name == settings.ServerName && s.Id != settings.PiwigoServerId).CountAsync(cancellation); if (count > 0) { - _logger.LogWarning("Server with name {SettingsServerName} already exists. Not updated", settings.ServerName); + _logger.Warning("Server with name {SettingsServerName} already exists. Not updated", settings.ServerName); AnsiConsole.MarkupLine( $"[maroon]You can not rename the server with id {settings.PiwigoServerId} to {settings.ServerName} as there is already a server with this name[/]"); return -1; @@ -36,7 +35,7 @@ internal class PiwigoUpdateCommand : CancellableAsyncCommand { private readonly IFileIndexer _fileIndexer; private readonly IFileSystemScanner _fileSystemScanner; - private readonly ILogger _logger; + private readonly ILogger _logger = Log.ForContext(); - public ScanCommand(ILogger logger, IFileIndexer fileIndexer, IFileSystemScanner fileSystemScanner) + public ScanCommand(IFileIndexer fileIndexer, IFileSystemScanner fileSystemScanner) { - _logger = logger; _fileIndexer = fileIndexer; _fileSystemScanner = fileSystemScanner; } @@ -32,7 +31,7 @@ internal class ScanCommand : CancellableAsyncCommand internal static async Task ScanDirectory(ILogger logger, IFileIndexer fileIndexer, IFileSystemScanner fileSystemScanner, int piwigoServerId, CancellationToken ct) { //TODO: check files for deletion -> files in db but no longer exist - logger.LogInformation("Starting scanner and remover"); + logger.Information("Starting scanner and remover"); var stopWatch = Stopwatch.StartNew(); var fileQueue = Channel.CreateUnbounded(); @@ -42,12 +41,12 @@ internal class ScanCommand : CancellableAsyncCommand await Task.WhenAll(scannerTask, indexerTask); stopWatch.Stop(); - logger.LogInformation("Processed {IndexerTotalFilesScanned} image files in {ElapsedTotalSeconds} seconds", fileIndexer.TotalFilesScanned, stopWatch.Elapsed.TotalSeconds); + logger.Information("Processed {IndexerTotalFilesScanned} image files in {ElapsedTotalSeconds} seconds", fileIndexer.TotalFilesScanned, stopWatch.Elapsed.TotalSeconds); foreach (var failedFilePath in fileIndexer.FailedFiles) { AnsiConsole.MarkupLine($"[red]Failed to index file {failedFilePath}[/]"); - logger.LogError("Failed to index file {FailedFilePath}", failedFilePath); + logger.Error("Failed to index file {FailedFilePath}", failedFilePath); } } diff --git a/PiwigoDirectorySync/Commands/SyncAlbumsCommand.cs b/PiwigoDirectorySync/Commands/SyncAlbumsCommand.cs index 2ca640f..8e8c554 100644 --- a/PiwigoDirectorySync/Commands/SyncAlbumsCommand.cs +++ b/PiwigoDirectorySync/Commands/SyncAlbumsCommand.cs @@ -1,8 +1,8 @@ using System.ComponentModel; using System.Diagnostics; -using Microsoft.Extensions.Logging; using PiwigoDirectorySync.Infrastructure; using PiwigoDirectorySync.Services; +using Serilog; using Spectre.Console.Cli; namespace PiwigoDirectorySync.Commands; @@ -10,24 +10,23 @@ namespace PiwigoDirectorySync.Commands; internal class SyncAlbumsCommand : CancellableAsyncCommand { private readonly IAlbumSynchronizer _albumSynchronizer; - private readonly ILogger _logger; + private readonly ILogger _logger = Log.ForContext(); - public SyncAlbumsCommand(ILogger logger, IAlbumSynchronizer albumSynchronizer) + public SyncAlbumsCommand(IAlbumSynchronizer albumSynchronizer) { - _logger = logger; _albumSynchronizer = albumSynchronizer; } protected override async Task ExecuteAsync(CommandContext context, SyncAlbumsSettings settings, CancellationToken cancellationToken) { - _logger.LogInformation("Starting album synchronization for piwigo server {SettingsPiwigoServerId}", settings.PiwigoServerId); + _logger.Information("Starting album synchronization for piwigo server {SettingsPiwigoServerId}", settings.PiwigoServerId); var stopWatch = Stopwatch.StartNew(); await _albumSynchronizer.SynchronizeAlbums(settings.PiwigoServerId, settings.CreateAlbumsOnServer, cancellationToken); stopWatch.Stop(); - _logger.LogInformation("Synchronized all albums with piwigo server {SettingsPiwigoServerId} in {ElapsedTotalSeconds} seconds", settings.PiwigoServerId, + _logger.Information("Synchronized all albums with piwigo server {SettingsPiwigoServerId} in {ElapsedTotalSeconds} seconds", settings.PiwigoServerId, stopWatch.Elapsed.TotalSeconds); return 0; diff --git a/PiwigoDirectorySync/Commands/SyncFullCommand.cs b/PiwigoDirectorySync/Commands/SyncFullCommand.cs index 5b22f83..db37a46 100644 --- a/PiwigoDirectorySync/Commands/SyncFullCommand.cs +++ b/PiwigoDirectorySync/Commands/SyncFullCommand.cs @@ -1,7 +1,7 @@ using System.Diagnostics; -using Microsoft.Extensions.Logging; using PiwigoDirectorySync.Infrastructure; using PiwigoDirectorySync.Services; +using Serilog; using Spectre.Console.Cli; namespace PiwigoDirectorySync.Commands; @@ -12,34 +12,32 @@ internal class SyncFullCommand : CancellableAsyncCommand _logger; + private readonly ILogger _logger = Log.ForContext(); - public SyncFullCommand(IAlbumSynchronizer albumSynchronizer, IFileIndexer fileIndexer, IFileSystemScanner fileSystemScanner, IImageSynchronizer imageSynchronizer, - ILogger logger) + public SyncFullCommand(IAlbumSynchronizer albumSynchronizer, IFileIndexer fileIndexer, IFileSystemScanner fileSystemScanner, IImageSynchronizer imageSynchronizer) { _albumSynchronizer = albumSynchronizer; _fileIndexer = fileIndexer; _fileSystemScanner = fileSystemScanner; _imageSynchronizer = imageSynchronizer; - _logger = logger; } protected override async Task ExecuteAsync(CommandContext context, SyncFullSettings settings, CancellationToken cancellationToken) { - _logger.LogInformation("Starting full synchronization for piwigo server {SettingsPiwigoServerId}", settings.PiwigoServerId); + _logger.Information("Starting full synchronization for piwigo server {SettingsPiwigoServerId}", settings.PiwigoServerId); var stopWatch = Stopwatch.StartNew(); - _logger.LogInformation("running file system scan"); + _logger.Information("running file system scan"); await ScanCommand.ScanDirectory(_logger, _fileIndexer, _fileSystemScanner, settings.PiwigoServerId, cancellationToken); - _logger.LogInformation("running album synchronization"); + _logger.Information("running album synchronization"); await _albumSynchronizer.SynchronizeAlbums(settings.PiwigoServerId, true, cancellationToken); - _logger.LogInformation("running image synchronization"); + _logger.Information("running image synchronization"); await _imageSynchronizer.SynchronizeImagesAsync(settings.PiwigoServerId, cancellationToken); stopWatch.Stop(); - _logger.LogInformation("Full synchronization for piwigo server {SettingsPiwigoServerId} finished in {ElapsedTotalSeconds} seconds", settings.PiwigoServerId, + _logger.Information("Full synchronization for piwigo server {SettingsPiwigoServerId} finished in {ElapsedTotalSeconds} seconds", settings.PiwigoServerId, stopWatch.Elapsed.TotalSeconds); return 0; diff --git a/PiwigoDirectorySync/Commands/SyncImagesCommand.cs b/PiwigoDirectorySync/Commands/SyncImagesCommand.cs index 21fe96b..9488461 100644 --- a/PiwigoDirectorySync/Commands/SyncImagesCommand.cs +++ b/PiwigoDirectorySync/Commands/SyncImagesCommand.cs @@ -1,7 +1,7 @@ using System.Diagnostics; -using Microsoft.Extensions.Logging; using PiwigoDirectorySync.Infrastructure; using PiwigoDirectorySync.Services; +using Serilog; using Spectre.Console.Cli; namespace PiwigoDirectorySync.Commands; @@ -9,23 +9,22 @@ namespace PiwigoDirectorySync.Commands; internal class SyncImagesCommand : CancellableAsyncCommand { private readonly IImageSynchronizer _imageSynchronizer; - private readonly ILogger _logger; + private readonly ILogger _logger = Log.ForContext(); - public SyncImagesCommand(ILogger logger, IImageSynchronizer imageSynchronizer) + public SyncImagesCommand(IImageSynchronizer imageSynchronizer) { - _logger = logger; _imageSynchronizer = imageSynchronizer; } protected override async Task ExecuteAsync(CommandContext context, SyncImagesSettings settings, CancellationToken cancellationToken) { - _logger.LogInformation("Starting image synchronization of piwigo server {SettingsPiwigoServerId}", settings.PiwigoServerId); + _logger.Information("Starting image synchronization of piwigo server {SettingsPiwigoServerId}", settings.PiwigoServerId); var stopWatch = Stopwatch.StartNew(); await _imageSynchronizer.SynchronizeImagesAsync(settings.PiwigoServerId, cancellationToken); stopWatch.Stop(); - _logger.LogInformation("Synchronized all images with piwigo server {SettingsPiwigoServerId} in {ElapsedTotalSeconds} seconds", settings.PiwigoServerId, + _logger.Information("Synchronized all images with piwigo server {SettingsPiwigoServerId} in {ElapsedTotalSeconds} seconds", settings.PiwigoServerId, stopWatch.Elapsed.TotalSeconds); return 0; diff --git a/PiwigoDirectorySync/Commands/UpdateDatabaseCommand.cs b/PiwigoDirectorySync/Commands/UpdateDatabaseCommand.cs index 2862dc2..d74a89e 100644 --- a/PiwigoDirectorySync/Commands/UpdateDatabaseCommand.cs +++ b/PiwigoDirectorySync/Commands/UpdateDatabaseCommand.cs @@ -1,7 +1,7 @@ using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging; using PiwigoDirectorySync.Infrastructure; using PiwigoDirectorySync.Persistence; +using Serilog; using Spectre.Console; using Spectre.Console.Cli; @@ -9,12 +9,11 @@ namespace PiwigoDirectorySync.Commands; internal class UpdateDatabaseCommand : CancellableAsyncCommand { - private readonly ILogger _logger; + private readonly ILogger _logger = Log.ForContext(); private readonly PersistenceContext _persistenceContext; - public UpdateDatabaseCommand(ILogger logger, PersistenceContext persistenceContext) + public UpdateDatabaseCommand(PersistenceContext persistenceContext) { - _logger = logger; _persistenceContext = persistenceContext; } @@ -31,7 +30,7 @@ internal class UpdateDatabaseCommand : CancellableAsyncCommand _logger; + private readonly ILogger _logger = Log.ForContext(); private readonly PersistenceContext _persistenceContext; private readonly IPiwigoClientFactory _piwigoClientFactory; - public AlbumSynchronizer(ILogger logger, IPiwigoClientFactory piwigoClientFactory, PersistenceContext persistenceContext) + public AlbumSynchronizer(IPiwigoClientFactory piwigoClientFactory, PersistenceContext persistenceContext) { - _logger = logger; _piwigoClientFactory = piwigoClientFactory; _persistenceContext = persistenceContext; } @@ -25,7 +24,7 @@ internal class AlbumSynchronizer : IAlbumSynchronizer var piwigoServer = await _persistenceContext.PiwigoServers.FindAsync(new object?[] { piwigoServerId }, ct); if (piwigoServer is null) { - _logger.LogError("Could not sync albums with piwigo server {PiwigoServerId}", piwigoServerId); + _logger.Error("Could not sync albums with piwigo server {PiwigoServerId}", piwigoServerId); return; } @@ -34,12 +33,12 @@ internal class AlbumSynchronizer : IAlbumSynchronizer await UpdatePiwigoAlbumsFromServerAsync(piwigoClient, piwigoServer, ct); if (createAlbumsOnServer) { - _logger.LogInformation("Creating missing albums on server {PiwigoServerId} - {PiwigoServerName}", piwigoServer.Id, piwigoServer.Name); + _logger.Information("Creating missing albums on server {PiwigoServerId} - {PiwigoServerName}", piwigoServer.Id, piwigoServer.Name); await AddMissingAlbumsToServerAsync(piwigoClient, piwigoServer, ct); } else { - _logger.LogInformation("Skipping album creation on server {PiwigoServerId} - {PiwigoServerName}", piwigoServer.Id, piwigoServer.Name); + _logger.Information("Skipping album creation on server {PiwigoServerId} - {PiwigoServerName}", piwigoServer.Id, piwigoServer.Name); } } @@ -69,7 +68,7 @@ internal class AlbumSynchronizer : IAlbumSynchronizer foreach (var serverAlbum in serverAlbums) { - _logger.LogInformation("Updating piwigo server album {ServerAlbumName} with piwigo id {ServerAlbumId}", serverAlbum.Name, serverAlbum.Id); + _logger.Information("Updating piwigo server album {ServerAlbumName} with piwigo id {ServerAlbumId}", serverAlbum.Name, serverAlbum.Id); var albumEntity = await GetOrAddPiwigoAlbumEntityFromServerAsync(piwigoServer, serverAlbum, serverAlbumDictionary, ct); if (serverAlbum.IdUpperCat.HasValue) @@ -88,7 +87,7 @@ internal class AlbumSynchronizer : IAlbumSynchronizer var albumEntity = await _persistenceContext.PiwigoAlbums.FindByServerIdAsync(piwigoServer.Id, serverAlbum.Id, ct); if (albumEntity != null) { - _logger.LogDebug("Found existing album {AlbumEntityName} with local id {AlbumEntityId} and piwigo server id {ServerAlbumId}", albumEntity.Name, albumEntity.Id, + _logger.Debug("Found existing album {AlbumEntityName} with local id {AlbumEntityId} and piwigo server id {ServerAlbumId}", albumEntity.Name, albumEntity.Id, albumEntity.ServerAlbumId); return albumEntity; } @@ -100,7 +99,7 @@ internal class AlbumSynchronizer : IAlbumSynchronizer if (albumEntity != null) { albumEntity.ServerAlbumId = serverAlbum.Id; - _logger.LogInformation("Linking existing album {AlbumEntityName} with local id {AlbumEntityId} to piwigo server id {ServerAlbumId}", albumEntity.Name, albumEntity.Id, + _logger.Information("Linking existing album {AlbumEntityName} with local id {AlbumEntityId} to piwigo server id {ServerAlbumId}", albumEntity.Name, albumEntity.Id, albumEntity.ServerAlbumId); return albumEntity; } @@ -113,7 +112,7 @@ internal class AlbumSynchronizer : IAlbumSynchronizer ServerAlbumId = serverAlbum.Id, Path = path }; - _logger.LogInformation("Adding piwigo album {AlbumEntityName} with local id {AlbumEntityId} and piwigo server id {ServerAlbumId}", albumEntity.Name, albumEntity.Id, + _logger.Information("Adding piwigo album {AlbumEntityName} with local id {AlbumEntityId} and piwigo server id {ServerAlbumId}", albumEntity.Name, albumEntity.Id, albumEntity.ServerAlbumId); _persistenceContext.PiwigoAlbums.Add(albumEntity); @@ -136,7 +135,7 @@ internal class AlbumSynchronizer : IAlbumSynchronizer parentId = currentParent.IdUpperCat ?? 0; } - _logger.LogDebug("Generated path {Path} for album with piwigo id {ServerAlbumId}", path, serverAlbum.Id); + _logger.Debug("Generated path {Path} for album with piwigo id {ServerAlbumId}", path, serverAlbum.Id); return path; } diff --git a/PiwigoDirectorySync/Services/FileIndexer.cs b/PiwigoDirectorySync/Services/FileIndexer.cs index 18d3b89..44f8715 100644 --- a/PiwigoDirectorySync/Services/FileIndexer.cs +++ b/PiwigoDirectorySync/Services/FileIndexer.cs @@ -2,22 +2,22 @@ using System.Threading.Channels; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; using PiwigoDirectorySync.Infrastructure; using PiwigoDirectorySync.Persistence; +using Serilog; namespace PiwigoDirectorySync.Services; internal class FileIndexer : IFileIndexer { private readonly ConcurrentBag _failedFiles = new(); - private readonly ILogger _logger; + private readonly ILogger _logger = Log.ForContext(); + private readonly IServiceProvider _serviceProvider; private int _totalFilesScanned; - public FileIndexer(ILogger logger, IServiceProvider serviceProvider) + public FileIndexer(IServiceProvider serviceProvider) { - _logger = logger; _serviceProvider = serviceProvider; } @@ -42,16 +42,16 @@ internal class FileIndexer : IFileIndexer { if (ct.IsCancellationRequested) { - _logger.LogWarning("Indexing cancelled"); + _logger.Warning("Indexing cancelled"); return; } - _logger.LogInformation("Indexing file {FullFilePath}", fullFilePath); + _logger.Information("Indexing file {FullFilePath}", fullFilePath); var fileInfo = new FileInfo(fullFilePath); if (!fileInfo.Exists) { - _logger.LogWarning("File {FullFilePath} not found", fullFilePath); + _logger.Warning("File {FullFilePath} not found", fullFilePath); _failedFiles.Add(fullFilePath); return; } @@ -82,7 +82,7 @@ internal class FileIndexer : IFileIndexer catch (Exception ex) { _failedFiles.Add(fullFilePath); - _logger.LogError(ex, "could not index file {FullFilePath}", fullFilePath); + _logger.Error(ex, "could not index file {FullFilePath}", fullFilePath); } } diff --git a/PiwigoDirectorySync/Services/FileSystemScanner.cs b/PiwigoDirectorySync/Services/FileSystemScanner.cs index ba44118..e9b59e6 100644 --- a/PiwigoDirectorySync/Services/FileSystemScanner.cs +++ b/PiwigoDirectorySync/Services/FileSystemScanner.cs @@ -1,18 +1,16 @@ using System.Threading.Channels; -using Microsoft.Extensions.Logging; using PiwigoDirectorySync.Persistence; +using Serilog; namespace PiwigoDirectorySync.Services; internal class FileSystemScanner : IFileSystemScanner { - private readonly ILogger _logger; + private readonly ILogger _logger = Log.ForContext(); private readonly PersistenceContext _persistenceContext; - - public FileSystemScanner(ILogger logger, PersistenceContext persistenceContext) + public FileSystemScanner(PersistenceContext persistenceContext) { - _logger = logger; _persistenceContext = persistenceContext; } @@ -21,7 +19,7 @@ internal class FileSystemScanner : IFileSystemScanner try { var piwigoServer = await _persistenceContext.PiwigoServers.GetByIdAsync(piwigoServerId, ct); - _logger.LogInformation("Scanning files for piwigo server {PiwigoServerName} in directory {PiwigoServerRootDirectory}", piwigoServer.Name, piwigoServer.RootDirectory); + _logger.Information("Scanning files for piwigo server {PiwigoServerName} in directory {PiwigoServerRootDirectory}", piwigoServer.Name, piwigoServer.RootDirectory); await ScanRootDirectory(fileQueue, new DirectoryInfo(piwigoServer.RootDirectory), ct); } @@ -33,7 +31,7 @@ internal class FileSystemScanner : IFileSystemScanner private async ValueTask ScanRootDirectory(Channel fileQueue, DirectoryInfo directory, CancellationToken ct) { - _logger.LogInformation("Scanning root directory {DirectoryFullName} for sidecars to delete", directory.FullName); + _logger.Information("Scanning root directory {DirectoryFullName} for sidecars to delete", directory.FullName); var parallelOptions = new ParallelOptions { CancellationToken = ct @@ -45,7 +43,7 @@ internal class FileSystemScanner : IFileSystemScanner { try { - _logger.LogInformation("Scanning directory {DirectoryFullName} for images", directory.FullName); + _logger.Information("Scanning directory {DirectoryFullName} for images", directory.FullName); var imageFiles = AppSettings.SupportedExtensions.SelectMany(ext => directory.GetFiles($"*.{ext}", SearchOption.TopDirectoryOnly)) .Select(f => f.FullName) @@ -53,19 +51,19 @@ internal class FileSystemScanner : IFileSystemScanner if (!imageFiles.Any()) { - _logger.LogDebug("No images in {DirectoryFullName} found, skipping", directory.FullName); + _logger.Debug("No images in {DirectoryFullName} found, skipping", directory.FullName); return; } foreach (var imageFile in imageFiles.Select(f => new FileInfo(f))) { - _logger.LogDebug("Found image {ImageFileFullName}, enqueue index", imageFile.FullName); + _logger.Debug("Found image {ImageFileFullName}, enqueue index", imageFile.FullName); await fileQueue.Writer.WriteAsync(imageFile.FullName, ct); } } catch (Exception ex) { - _logger.LogError(ex, "could not scan directory {DirectoryFullName}", directory.FullName); + _logger.Error(ex, "could not scan directory {DirectoryFullName}", directory.FullName); } } diff --git a/PiwigoDirectorySync/Services/ImageSynchronizer.cs b/PiwigoDirectorySync/Services/ImageSynchronizer.cs index 736b5b0..0834342 100644 --- a/PiwigoDirectorySync/Services/ImageSynchronizer.cs +++ b/PiwigoDirectorySync/Services/ImageSynchronizer.cs @@ -1,23 +1,22 @@ using Flurl.Http; using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging; using Piwigo.Client; using Piwigo.Client.Albums; using Piwigo.Client.Images; using PiwigoDirectorySync.Infrastructure; using PiwigoDirectorySync.Persistence; +using Serilog; namespace PiwigoDirectorySync.Services; internal class ImageSynchronizer : IImageSynchronizer { - private readonly ILogger _logger; + private readonly ILogger _logger = Log.ForContext(); private readonly PersistenceContext _persistenceContext; private readonly IPiwigoClientFactory _piwigoClientFactory; - public ImageSynchronizer(ILogger logger, IPiwigoClientFactory piwigoClientFactory, PersistenceContext persistenceContext) + public ImageSynchronizer(IPiwigoClientFactory piwigoClientFactory, PersistenceContext persistenceContext) { - _logger = logger; _piwigoClientFactory = piwigoClientFactory; _persistenceContext = persistenceContext; } @@ -27,11 +26,11 @@ internal class ImageSynchronizer : IImageSynchronizer var piwigoServer = await _persistenceContext.PiwigoServers.FindAsync(new object?[] { piwigoServerId }, ct); if (piwigoServer is null) { - _logger.LogError("Could not sync images with piwigo server {PiwigoServerId}", piwigoServerId); + _logger.Error("Could not sync images with piwigo server {PiwigoServerId}", piwigoServerId); return; } - _logger.LogInformation("Downloading missing images of piwigo server {PiwigoServerName} using base path {PiwigoServerRootDirectory}", piwigoServer.Name, + _logger.Information("Downloading missing images of piwigo server {PiwigoServerName} using base path {PiwigoServerRootDirectory}", piwigoServer.Name, piwigoServer.RootDirectory); var piwigoClient = await _piwigoClientFactory.GetPiwigoClientAsync(piwigoServer, ct); @@ -43,7 +42,7 @@ internal class ImageSynchronizer : IImageSynchronizer var albumInfos = await piwigoClient.Album.GetListAsync(albumId, false, false, ThumbnailSize.Thumb, ct); var albumInfo = albumInfos.First(); - _logger.LogInformation("Starting downloads for album {AlbumInfoName}", albumInfo.Name); + _logger.Information("Starting downloads for album {AlbumInfoName}", albumInfo.Name); await DownloadImagesForAlbumAsync(piwigoClient, piwigoServer, albumId, albumInfo, ct); } @@ -54,11 +53,11 @@ internal class ImageSynchronizer : IImageSynchronizer var piwigoServer = await _persistenceContext.PiwigoServers.FindAsync(new object?[] { piwigoServerId }, ct); if (piwigoServer is null) { - _logger.LogError("Could not sync images with piwigo server {PiwigoServerId}", piwigoServerId); + _logger.Error("Could not sync images with piwigo server {PiwigoServerId}", piwigoServerId); return; } - _logger.LogInformation("Synchronizing images of piwigo server {PiwigoServerName} using base path {PiwigoServerRootDirectory}", piwigoServer.Name, + _logger.Information("Synchronizing images of piwigo server {PiwigoServerName} using base path {PiwigoServerRootDirectory}", piwigoServer.Name, piwigoServer.RootDirectory); var piwigoClient = await _piwigoClientFactory.GetPiwigoClientAsync(piwigoServer, ct); @@ -72,7 +71,7 @@ internal class ImageSynchronizer : IImageSynchronizer { if (albumInfo.NbImages is null or <= 0) { - _logger.LogInformation("No images to download for empty album {AlbumId} / {AlbumInfoName}", albumId, albumInfo.Name); + _logger.Information("No images to download for empty album {AlbumId} / {AlbumInfoName}", albumId, albumInfo.Name); return; } @@ -99,7 +98,7 @@ internal class ImageSynchronizer : IImageSynchronizer var localAlbum = await _persistenceContext.PiwigoAlbums.FindByServerIdAsync(piwigoServer.Id, albumInfo.Id, ct); if (localAlbum is null) { - _logger.LogWarning("Could not add image {ImageId} / {ImageFile}: album with server id {AlbumInfoId} / {AlbumInfoName} not found", image.Id, image.File, albumInfo.Id, + _logger.Warning("Could not add image {ImageId} / {ImageFile}: album with server id {AlbumInfoId} / {AlbumInfoName} not found", image.Id, image.File, albumInfo.Id, albumInfo.Name); return; } @@ -109,7 +108,7 @@ internal class ImageSynchronizer : IImageSynchronizer var fileInfo = new FileInfo(Path.Combine(piwigoServer.RootDirectory, localImage.FilePath)); if (fileInfo.Exists) { - _logger.LogWarning("Tried to download image {ImageFile} but it already exists", image.File); + _logger.Warning("Tried to download image {ImageFile} but it already exists", image.File); return; } @@ -144,7 +143,7 @@ internal class ImageSynchronizer : IImageSynchronizer .Where(i => i.ServerImageId != null && i.Album.ServerId == piwigoServer.Id && i.UploadRequired) .ToListAsync(ct); - _logger.LogInformation("Updating {Count} images", imagesToUpload.Count); + _logger.Information("Updating {Count} images", imagesToUpload.Count); foreach (var imageEntity in imagesToUpload) { @@ -155,7 +154,7 @@ internal class ImageSynchronizer : IImageSynchronizer imageEntity.ServerImageId = imageUploaded.ImageId; imageEntity.UploadRequired = false; - _logger.LogInformation("Updated image {ImageEntityName} ({ImageEntityId}) on piwigo server with id {ImageEntityServerImageId}", imageEntity.Name, imageEntity.Id, + _logger.Information("Updated image {ImageEntityName} ({ImageEntityId}) on piwigo server with id {ImageEntityServerImageId}", imageEntity.Name, imageEntity.Id, imageEntity.ServerImageId); await _persistenceContext.SaveChangesAsync(ct); @@ -168,7 +167,7 @@ internal class ImageSynchronizer : IImageSynchronizer .Where(i => i.ServerImageId == null && i.Album.ServerId == piwigoServer.Id && i.Album.ServerAlbumId != null) .ToListAsync(ct); - _logger.LogInformation("Uploading {Count} images", imagesToUpload.Count); + _logger.Information("Uploading {Count} images", imagesToUpload.Count); foreach (var imageEntity in imagesToUpload) { @@ -179,7 +178,7 @@ internal class ImageSynchronizer : IImageSynchronizer imageEntity.ServerImageId = imageUploaded.ImageId; imageEntity.UploadRequired = false; - _logger.LogInformation("Uploaded image {ImageEntityName} ({ImageEntityId}) to piwigo server with id {ImageEntityServerImageId}", imageEntity.Name, imageEntity.Id, + _logger.Information("Uploaded image {ImageEntityName} ({ImageEntityId}) to piwigo server with id {ImageEntityServerImageId}", imageEntity.Name, imageEntity.Id, imageEntity.ServerImageId); await _persistenceContext.SaveChangesAsync(ct); @@ -205,7 +204,7 @@ internal class ImageSynchronizer : IImageSynchronizer .Where(i => i.ServerImageId == null && i.Album.ServerId == piwigoServer.Id) .ToListAsync(ct); - _logger.LogInformation("Checking {Count} images if they exist", imagesToSearch.Count); + _logger.Information("Checking {Count} images if they exist", imagesToSearch.Count); var md5SumsToCheck = imagesToSearch.Where(i => i.Md5Sum != null).DistinctBy(i => i.Md5Sum).ToDictionary(i => i.Md5Sum!, i => i, StringComparer.OrdinalIgnoreCase); @@ -221,7 +220,7 @@ internal class ImageSynchronizer : IImageSynchronizer imageEntity.ServerImageId = existingImage.Value; imageEntity.UploadRequired = false; - _logger.LogInformation("Found image {ImageEntityName} ({ImageEntityId}) on piwigo server with id {ImageEntityServerImageId}", imageEntity.Name, imageEntity.Id, + _logger.Information("Found image {ImageEntityName} ({ImageEntityId}) on piwigo server with id {ImageEntityServerImageId}", imageEntity.Name, imageEntity.Id, imageEntity.ServerImageId); }