Add configurable logging tuning options

Introduced `LoggingTuningOptions` to centralize runtime logging configurations, including EF Core and Serilog request logging.

- Added `EfCoreLoggingOptions` for SQL logging, log levels, sensitive data logging, and detailed errors.
- Added `RequestLoggingOptions` for enabling/disabling Serilog HTTP request logging and excluding specific path prefixes.
- Updated `Program.cs` to dynamically configure EF Core and request logging based on `LoggingTuningOptions`.
- Added `LoggingTuning` section to `appsettings.json` and `appsettings.Development.json` for externalized configuration.
- Removed hardcoded EF Core logging settings for improved flexibility and maintainability.
This commit is contained in:
2026-11-30 09:53:34 +01:00
parent e4ce91ee8b
commit 9a986e1b9b
4 changed files with 147 additions and 5 deletions

View File

@@ -0,0 +1,67 @@
namespace EnvelopeGenerator.Server.Options;
using Microsoft.Extensions.Logging;
/// <summary>
/// Runtime logging tuning options loaded from configuration.
/// </summary>
public sealed class LoggingTuningOptions
{
/// <summary>
/// Configuration section name in appsettings.json.
/// </summary>
public const string SectionName = "LoggingTuning";
/// <summary>
///
/// </summary>
public EfCoreLoggingOptions EfCore { get; set; } = new();
/// <summary>
///
/// </summary>
public RequestLoggingOptions RequestLogging { get; set; } = new();
}
/// <summary>
///
/// </summary>
public sealed class EfCoreLoggingOptions
{
/// <summary>
/// Enables EF Core SQL/command logging pipeline.
/// </summary>
public bool EnableSqlLogging { get; set; } = true;
/// <summary>
/// Minimum EF Core event level to emit to logs.
/// </summary>
public LogLevel MinimumLevel { get; set; } = LogLevel.Warning;
/// <summary>
/// Includes EF parameter values in logs.
/// Keep disabled outside development.
/// </summary>
public bool EnableSensitiveDataLogging { get; set; }
/// <summary>
/// Enables richer EF error details.
/// </summary>
public bool EnableDetailedErrors { get; set; } = true;
}
/// <summary>
///
/// </summary>
public sealed class RequestLoggingOptions
{
/// <summary>
/// Enables Serilog HTTP request logging middleware.
/// </summary>
public bool Enabled { get; set; } = true;
/// <summary>
/// Request path prefixes to suppress from request logs (for example: /health, /_framework).
/// </summary>
public string[] ExcludedPathPrefixes { get; set; } = [];
}

View File

@@ -372,6 +372,11 @@ try
builder.Services.Configure<SerilogUiAccessOptions>(config.GetSection(SerilogUiAccessOptions.SectionName));
builder.Services.Configure<BackgroundServicesOptions>(config.GetSection(BackgroundServicesOptions.SectionName));
builder.Services.Configure<WindowsServiceHostingOptions>(config.GetSection(WindowsServiceHostingOptions.SectionName));
builder.Services.Configure<LoggingTuningOptions>(config.GetSection(LoggingTuningOptions.SectionName));
var loggingTuningOptions = config
.GetSection(LoggingTuningOptions.SectionName)
.Get<LoggingTuningOptions>() ?? new LoggingTuningOptions();
// Distributed Cache - SQL Server
builder.Services.AddDistributedSqlServerCache(options =>
@@ -393,10 +398,27 @@ try
opt.AddDbContext((provider, options) =>
{
var logger = provider.GetRequiredService<ILogger<EGDbContext>>();
options.UseSqlServer(connStr)
.LogTo(log => logger.LogInformation("{log}", log), Microsoft.Extensions.Logging.LogLevel.Trace)
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
options.UseSqlServer(connStr);
if (loggingTuningOptions.EfCore.EnableSqlLogging)
{
options.LogTo(
log => logger.Log(
loggingTuningOptions.EfCore.MinimumLevel,
"{log}",
log),
loggingTuningOptions.EfCore.MinimumLevel);
}
if (loggingTuningOptions.EfCore.EnableSensitiveDataLogging)
{
options.EnableSensitiveDataLogging();
}
if (loggingTuningOptions.EfCore.EnableDetailedErrors)
{
options.EnableDetailedErrors();
}
});
opt.AddSQLExecutor(executor => executor.ConnectionString = connStr);
})
@@ -482,7 +504,28 @@ try
// Exception handling middleware for API controllers
app.UseMiddleware<EnvelopeGenerator.Server.Middleware.ExceptionHandlingMiddleware>();
app.UseSerilogRequestLogging();
if (loggingTuningOptions.RequestLogging.Enabled)
{
app.UseSerilogRequestLogging(options =>
{
options.GetLevel = (httpContext, _, ex) =>
{
if (ex is not null || httpContext.Response.StatusCode >= StatusCodes.Status500InternalServerError)
{
return Serilog.Events.LogEventLevel.Error;
}
var path = httpContext.Request.Path;
if (loggingTuningOptions.RequestLogging.ExcludedPathPrefixes.Any(prefix =>
path.StartsWithSegments(prefix, StringComparison.OrdinalIgnoreCase)))
{
return Serilog.Events.LogEventLevel.Verbose;
}
return Serilog.Events.LogEventLevel.Information;
};
});
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())

View File

@@ -22,6 +22,22 @@
}
}
},
"LoggingTuning": {
"EfCore": {
"EnableSqlLogging": true,
"MinimumLevel": "Information",
"EnableSensitiveDataLogging": true,
"EnableDetailedErrors": true
},
"RequestLogging": {
"Enabled": true,
"ExcludedPathPrefixes": [
"/_framework",
"/_blazor",
"/favicon.ico"
]
}
},
"AuthClientParams": {
"Url": "http://172.24.12.39:9090/auth-hub",
"PublicKeys": [

View File

@@ -36,6 +36,22 @@
}
}
},
"LoggingTuning": {
"EfCore": {
"EnableSqlLogging": true,
"MinimumLevel": "Warning",
"EnableSensitiveDataLogging": true,
"EnableDetailedErrors": true
},
"RequestLogging": {
"Enabled": true,
"ExcludedPathPrefixes": [
"/_framework",
"/_blazor",
"/favicon.ico"
]
}
},
"AllowedHosts": "*",
"AllowedOrigins": [
"http://localhost:4200",