From 73abb7bf17a4f1f0635c9d59fbf3c3c83cf43ccd Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Tue, 11 Nov 2025 13:32:00 +0100 Subject: [PATCH] sqlite provider: logging for db ops --- zero.Sqlite/Operations/DbOperations.Delete.cs | 4 ++++ zero.Sqlite/Operations/DbOperations.Write.cs | 18 +++++++++++++++--- zero.Sqlite/Operations/DbOperations.cs | 6 +++++- zero/Media/MediaCreator.cs | 17 +++++++---------- zero/Media/StaticMediaCreator.cs | 3 ++- 5 files changed, 33 insertions(+), 15 deletions(-) diff --git a/zero.Sqlite/Operations/DbOperations.Delete.cs b/zero.Sqlite/Operations/DbOperations.Delete.cs index 75dd9495..3f443709 100644 --- a/zero.Sqlite/Operations/DbOperations.Delete.cs +++ b/zero.Sqlite/Operations/DbOperations.Delete.cs @@ -1,5 +1,6 @@  using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using ServiceStack.OrmLite; using zero.Models; @@ -19,6 +20,7 @@ public partial class DbOperations : IDbOperations if (model == null) { + Logger.LogWarning("Could not delete entity (model is null) for type {type}", typeof(T)); return Result.Fail("@errors.ondelete.idnotfound"); } @@ -31,6 +33,8 @@ public partial class DbOperations : IDbOperations await Db.DeleteByIdAsync(model.Id); } + Logger.LogInformation("{id} ({type}) successfully deleted", typeof(T), model.Id); + return Result.Success(); } } \ No newline at end of file diff --git a/zero.Sqlite/Operations/DbOperations.Write.cs b/zero.Sqlite/Operations/DbOperations.Write.cs index 200fd681..02fcfa9d 100644 --- a/zero.Sqlite/Operations/DbOperations.Write.cs +++ b/zero.Sqlite/Operations/DbOperations.Write.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using FluentValidation.Results; +using Microsoft.Extensions.Logging; using ServiceStack.OrmLite; using zero.Models; using zero.Extensions; @@ -22,15 +23,14 @@ public partial class DbOperations : IDbOperations { if (model == null) { + Logger.LogWarning("Could not create/update entity (model is null) for type {type}", typeof(T)); return Result.Fail("@errors.onsave.empty"); } - T previousModel = null; - // check if the Id for a model already exists if (!model.Id.IsNullOrEmpty()) { - previousModel = await Db.SingleByIdAsync(model.Id); + T previousModel = await Db.SingleByIdAsync(model.Id); if (update && previousModel == null) { @@ -47,6 +47,7 @@ public partial class DbOperations : IDbOperations { if (!Flavors.Exists(flavorModel.Flavor)) { + Logger.LogWarning("Flavor {flavor} not found for type {type}", flavorModel.Flavor, typeof(T)); return Result.Fail("@errors.onsave.flavornotfound"); } } @@ -61,6 +62,7 @@ public partial class DbOperations : IDbOperations if (!validation.IsValid) { + Logger.LogInformation("Validation failed for {id} ({errors})", model.Id, validation.Errors); return Result.Fail(validation); } } @@ -74,6 +76,16 @@ public partial class DbOperations : IDbOperations // store our model await Db.SaveAsync(model); + string action = update ? "Updated" : "Created"; + if (model is ZeroEntity zeroEntity) + { + Logger.LogInformation(action + " {id} with name {name}", model.Id, zeroEntity.Name); + } + else + { + Logger.LogInformation(action + " {id}", model.Id); + } + return Result.Success(model); } } \ No newline at end of file diff --git a/zero.Sqlite/Operations/DbOperations.cs b/zero.Sqlite/Operations/DbOperations.cs index ad170dd9..7ac6cc37 100644 --- a/zero.Sqlite/Operations/DbOperations.cs +++ b/zero.Sqlite/Operations/DbOperations.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.DependencyInjection; using System.Linq.Expressions; using System.Security.Claims; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using ServiceStack.OrmLite; using zero.Context; using zero.Models; @@ -24,13 +25,16 @@ public partial class DbOperations : IDbOperations protected IDbConnection Db { get; private set; } + protected ILogger Logger { get; private set; } - public DbOperations(StoreContext context, IDbConnection db) + + public DbOperations(StoreContext context, IDbConnection db, ILogger logger) { Context = context.Context; Services = context.Services; Flavors = context.Options.For(); Db = db; + Logger = logger; } diff --git a/zero/Media/MediaCreator.cs b/zero/Media/MediaCreator.cs index 04c00f02..0f7f3866 100644 --- a/zero/Media/MediaCreator.cs +++ b/zero/Media/MediaCreator.cs @@ -8,21 +8,15 @@ using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; using System.IO; +using Microsoft.Extensions.Logging; namespace zero.Media; -public class MediaCreator : IMediaCreator +public class MediaCreator(IMediaFileSystem fileSystem, IZeroOptions options, ILogger logger) : IMediaCreator { - protected IMediaFileSystem FileSystem { get; set; } + protected IMediaFileSystem FileSystem { get; set; } = fileSystem; - protected MediaOptions Options { get; set; } - - - public MediaCreator(IMediaFileSystem fileSystem, IZeroOptions options) - { - FileSystem = fileSystem; - Options = options.For(); - } + protected MediaOptions Options { get; set; } = options.For(); /// @@ -61,6 +55,8 @@ public class MediaCreator : IMediaCreator // we need file metadata to get info about file size and the physical path for image modification IFileMeta fileInfo = await FileSystem.GetFileInfo(model.Path, cancellationToken); model.Size = fileInfo.Length; + + logger.LogInformation("Created media item {path} ({size})", model.Path, model.Size.GetFileSize()); if (isImage) { @@ -130,6 +126,7 @@ public class MediaCreator : IMediaCreator { string directoryName = GetNewDirectoryName(); await FileSystem.CreateDirectory(directoryName, cancellationToken); + logger.LogDebug("Created media directory {name}", directoryName); return directoryName; } catch (FileSystemException ex) when (ex.Message.Contains("already exists")) diff --git a/zero/Media/StaticMediaCreator.cs b/zero/Media/StaticMediaCreator.cs index 45ba3a42..bff74aa7 100644 --- a/zero/Media/StaticMediaCreator.cs +++ b/zero/Media/StaticMediaCreator.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.FileProviders; using SixLabors.ImageSharp; using System.IO; +using Microsoft.Extensions.Logging; namespace zero.Media; @@ -13,7 +14,7 @@ public class StaticMediaCreator : MediaCreator, IStaticMediaCreator protected IFileProvider FileProvider { get; set; } - public StaticMediaCreator(IMediaFileSystem fileSystem, IZeroOptions options, IWebHostEnvironment hostingEnvironment, MediaMetadataCache cacheProvider) : base(fileSystem, options) + public StaticMediaCreator(IMediaFileSystem fileSystem, IZeroOptions options, IWebHostEnvironment hostingEnvironment, MediaMetadataCache cacheProvider, ILogger logger) : base(fileSystem, options, logger) { FileProvider = hostingEnvironment.WebRootFileProvider; Cache = cacheProvider.Cache;