sqlite provider: logging for db ops

This commit is contained in:
2025-11-11 13:32:00 +01:00
parent f7858b296a
commit 73abb7bf17
5 changed files with 33 additions and 15 deletions
@@ -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<T>.Fail("@errors.ondelete.idnotfound");
}
@@ -31,6 +33,8 @@ public partial class DbOperations : IDbOperations
await Db.DeleteByIdAsync<T>(model.Id);
}
Logger.LogInformation("{id} ({type}) successfully deleted", typeof(T), model.Id);
return Result<T>.Success();
}
}
+15 -3
View File
@@ -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<T>.Fail("@errors.onsave.empty");
}
T previousModel = null;
// check if the Id for a model already exists
if (!model.Id.IsNullOrEmpty())
{
previousModel = await Db.SingleByIdAsync<T>(model.Id);
T previousModel = await Db.SingleByIdAsync<T>(model.Id);
if (update && previousModel == null)
{
@@ -47,6 +47,7 @@ public partial class DbOperations : IDbOperations
{
if (!Flavors.Exists<T>(flavorModel.Flavor))
{
Logger.LogWarning("Flavor {flavor} not found for type {type}", flavorModel.Flavor, typeof(T));
return Result<T>.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<T>.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<T>.Success(model);
}
}
+5 -1
View File
@@ -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<IDbOperations> Logger { get; private set; }
public DbOperations(StoreContext context, IDbConnection db)
public DbOperations(StoreContext context, IDbConnection db, ILogger<IDbOperations> logger)
{
Context = context.Context;
Services = context.Services;
Flavors = context.Options.For<FlavorOptions>();
Db = db;
Logger = logger;
}
+7 -10
View File
@@ -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<IMediaCreator> 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<MediaOptions>();
}
protected MediaOptions Options { get; set; } = options.For<MediaOptions>();
/// <inheritdoc />
@@ -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"))
+2 -1
View File
@@ -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<IMediaCreator> logger) : base(fileSystem, options, logger)
{
FileProvider = hostingEnvironment.WebRootFileProvider;
Cache = cacheProvider.Cache;