Update DIExtensions for enhanced DbContext configuration

Modified the `AddEnvelopeGeneratorInfrastructureServices` method to accept a service provider, allowing for more complex DbContext configurations. Added a using directive for `Microsoft.Extensions.DependencyInjection` in `Program.cs`. Updated the method call to utilize the new signature, enabling logging of SQL commands and sensitive data during database operations.
This commit is contained in:
Developer 02 2025-05-12 16:35:48 +02:00
parent 456c591fae
commit 05867cc645
2 changed files with 10 additions and 2 deletions

View File

@ -33,7 +33,7 @@ public static class DIExtensions
/// will be created per HTTP request (or per scope) within the dependency injection container. /// will be created per HTTP request (or per scope) within the dependency injection container.
/// </remarks> /// </remarks>
public static IServiceCollection AddEnvelopeGeneratorInfrastructureServices(this IServiceCollection services, public static IServiceCollection AddEnvelopeGeneratorInfrastructureServices(this IServiceCollection services,
Action<DbContextOptionsBuilder>? dbContextOptions = null, Action<IServiceProvider, DbContextOptionsBuilder>? dbContextOptions = null,
IConfiguration? sqlExecutorConfiguration = null, IConfiguration? sqlExecutorConfiguration = null,
Action<SQLExecutorParams>? sqlExecutorConfigureOptions = null) Action<SQLExecutorParams>? sqlExecutorConfigureOptions = null)
{ {

View File

@ -17,6 +17,8 @@ using EnvelopeGenerator.Web.Sanitizers;
using EnvelopeGenerator.Application.Contracts.Services; using EnvelopeGenerator.Application.Contracts.Services;
using EnvelopeGenerator.Web.Models.Annotation; using EnvelopeGenerator.Web.Models.Annotation;
using DigitalData.UserManager.DependencyInjection; using DigitalData.UserManager.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using System;
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger(); var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
logger.Info("Logging initialized!"); logger.Info("Logging initialized!");
@ -98,7 +100,13 @@ try
}); });
// Add envelope generator services // Add envelope generator services
builder.Services.AddEnvelopeGeneratorInfrastructureServices(options => options.UseSqlServer(connStr)); builder.Services.AddEnvelopeGeneratorInfrastructureServices((provider, options) =>
{
var logger = provider.GetRequiredService<ILogger<EGDbContext>>();
options.UseSqlServer(connStr)
.LogTo(log => logger.LogInformation("{log}", log), Microsoft.Extensions.Logging.LogLevel.Trace)
.EnableSensitiveDataLogging();
});
builder.Services.AddEnvelopeGeneratorServices(config); builder.Services.AddEnvelopeGeneratorServices(config);