namespace Webzine.WebApplication.Interceptors;
using System.Data.Common;
using System.Diagnostics;
using Configuration;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Options;
///
/// Intercepteur EF Core qui journalise uniquement les commandes SQL dépassant le seuil configuré.
/// Remonte la pile d'appels pour identifier la méthode repository (Webzine.Repository.*) à l'origine de la requête.
///
///
///
/// Références :
///
///
/// EF Core interceptors (doc officielle) :
///
///
///
/// API :
///
///
///
/// Exemple de slow-query interceptor (SO) :
///
///
///
/// pour remonter l'appelant :
///
///
///
/// Enregistrement via AddInterceptors :
///
///
///
///
///
public class EfSlowQueryInterceptor : DbCommandInterceptor
{
private readonly ILogger logger;
private readonly int seuilMs;
///
/// Initializes a new instance of the class.
///
/// Le service de journalisation injecté pour suivre les opérations de l'intercepteur.
/// Les options de performance EF injectées pour récupérer le seuil de lenteur configuré.
public EfSlowQueryInterceptor(ILogger logger, IOptions options)
{
this.logger = logger;
this.seuilMs = options.Value.SeuilMs;
this.logger.LogDebug("[EfSlowQueryInterceptor] Constructeur appelé — seuil : {SeuilMs} ms.", this.seuilMs);
}
///
public override DbDataReader ReaderExecuted(DbCommand command, CommandExecutedEventData eventData, DbDataReader result)
{
this.JournaliserSiLent(eventData.Duration);
return result;
}
///
public override ValueTask ReaderExecutedAsync(DbCommand command, CommandExecutedEventData eventData, DbDataReader result, CancellationToken cancellationToken = default)
{
this.JournaliserSiLent(eventData.Duration);
return ValueTask.FromResult(result);
}
///
public override int NonQueryExecuted(DbCommand command, CommandExecutedEventData eventData, int result)
{
this.JournaliserSiLent(eventData.Duration);
return result;
}
///
public override ValueTask NonQueryExecutedAsync(DbCommand command, CommandExecutedEventData eventData, int result, CancellationToken cancellationToken = default)
{
this.JournaliserSiLent(eventData.Duration);
return ValueTask.FromResult(result);
}
///
public override object? ScalarExecuted(DbCommand command, CommandExecutedEventData eventData, object? result)
{
this.JournaliserSiLent(eventData.Duration);
return result;
}
///
public override ValueTask