fix logging

This commit is contained in:
2026-07-08 15:44:42 -04:00
parent 4e6bf45701
commit df035a069d
@@ -18,6 +18,7 @@ using MediaBrowser.Controller.Configuration;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using JellyfinDbProviderFactory = System.Func<System.IServiceProvider, Jellyfin.Database.Implementations.IJellyfinDatabaseProvider>; using JellyfinDbProviderFactory = System.Func<System.IServiceProvider, Jellyfin.Database.Implementations.IJellyfinDatabaseProvider>;
/// <summary> /// <summary>
@@ -25,6 +26,35 @@ using JellyfinDbProviderFactory = System.Func<System.IServiceProvider, Jellyfin.
/// </summary> /// </summary>
public static class ServiceCollectionExtensions public static class ServiceCollectionExtensions
{ {
/// <summary>
/// Determines whether detailed EF Core logging should be enabled based on the configured log level.
/// </summary>
/// <param name="configuration">The application configuration.</param>
/// <returns>True if the log level for Microsoft.EntityFrameworkCore.Database.Command is Debug or lower; otherwise false.</returns>
private static bool ShouldEnableDetailedEFCoreLogging(IConfiguration configuration)
{
// Check if detailed EF Core logging is explicitly enabled in configuration
// Read the log level override for Entity Framework Core command logging
var efCoreLogLevelString = configuration["Serilog:MinimumLevel:Override:Microsoft.EntityFrameworkCore.Database.Command"];
if (string.IsNullOrWhiteSpace(efCoreLogLevelString))
{
// If not explicitly set, check the default minimum level
efCoreLogLevelString = configuration["Serilog:MinimumLevel:Default"] ?? "Information";
}
// Parse the log level string to LogLevel enum
if (Enum.TryParse<LogLevel>(efCoreLogLevelString, ignoreCase: true, out var logLevel))
{
// Enable detailed logging if the level is Debug or lower (more verbose)
// LogLevel order: Trace=0, Debug=1, Information=2, Warning=3, Error=4, Critical=5, None=6
return logLevel <= LogLevel.Debug;
}
// Default to false if we can't parse the log level
return false;
}
private static IEnumerable<Type> DatabaseProviderTypes() private static IEnumerable<Type> DatabaseProviderTypes()
{ {
yield return typeof(PostgresDatabaseProvider); yield return typeof(PostgresDatabaseProvider);
@@ -170,9 +200,15 @@ public static class ServiceCollectionExtensions
lockingBehavior.Initialise(opt); lockingBehavior.Initialise(opt);
var loggerFactory = serviceProvider.GetService<Microsoft.Extensions.Logging.ILoggerFactory>(); var loggerFactory = serviceProvider.GetService<Microsoft.Extensions.Logging.ILoggerFactory>();
opt.UseLoggerFactory(loggerFactory) opt.UseLoggerFactory(loggerFactory);
.EnableSensitiveDataLogging()
// Only enable sensitive data logging and detailed errors if the EF Core command log level
// is set to Debug or lower, respecting the logging configuration from logging.json
if (ShouldEnableDetailedEFCoreLogging(configuration))
{
opt.EnableSensitiveDataLogging()
.EnableDetailedErrors(); .EnableDetailedErrors();
}
}); });
return serviceCollection; return serviceCollection;