fix logging
This commit is contained in:
@@ -18,6 +18,7 @@ using MediaBrowser.Controller.Configuration;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using JellyfinDbProviderFactory = System.Func<System.IServiceProvider, Jellyfin.Database.Implementations.IJellyfinDatabaseProvider>;
|
||||
|
||||
/// <summary>
|
||||
@@ -25,6 +26,35 @@ using JellyfinDbProviderFactory = System.Func<System.IServiceProvider, Jellyfin.
|
||||
/// </summary>
|
||||
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()
|
||||
{
|
||||
yield return typeof(PostgresDatabaseProvider);
|
||||
@@ -170,9 +200,15 @@ public static class ServiceCollectionExtensions
|
||||
lockingBehavior.Initialise(opt);
|
||||
|
||||
var loggerFactory = serviceProvider.GetService<Microsoft.Extensions.Logging.ILoggerFactory>();
|
||||
opt.UseLoggerFactory(loggerFactory)
|
||||
.EnableSensitiveDataLogging()
|
||||
.EnableDetailedErrors();
|
||||
opt.UseLoggerFactory(loggerFactory);
|
||||
|
||||
// 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();
|
||||
}
|
||||
});
|
||||
|
||||
return serviceCollection;
|
||||
|
||||
Reference in New Issue
Block a user