using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Diagnostics; using Microsoft.EntityFrameworkCore.Diagnostics; using YiSha.Util; namespace YiSha.Data.EF { /// /// Sql执行拦截器 /// public class DbCommandCustomInterceptor : DbCommandInterceptor { public async override Task> NonQueryExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default) { var obj = await base.NonQueryExecutingAsync(command, eventData, result, cancellationToken); return obj; } public async override Task NonQueryExecutedAsync(DbCommand command, CommandExecutedEventData eventData, int result, CancellationToken cancellationToken = default) { if (eventData.Duration.TotalMilliseconds >= GlobalContext.SystemConfig.DBSlowSqlLogTime * 1000) { LogHelper.Warn("耗时的Sql:" + command.GetCommandText()); } int val = await base.NonQueryExecutedAsync(command, eventData, result, cancellationToken); return val; } public async override Task> ScalarExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default) { var obj = await base.ScalarExecutingAsync(command, eventData, result, cancellationToken); return obj; } public async override Task ScalarExecutedAsync(DbCommand command, CommandExecutedEventData eventData, object result, CancellationToken cancellationToken = default) { if (eventData.Duration.TotalMilliseconds >= GlobalContext.SystemConfig.DBSlowSqlLogTime * 1000) { LogHelper.Warn("耗时的Sql:" + command.GetCommandText()); } var obj = await base.ScalarExecutedAsync(command, eventData, result, cancellationToken); return obj; } public async override Task> ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default) { var obj = await base.ReaderExecutingAsync(command, eventData, result, cancellationToken); return obj; } public async override Task ReaderExecutedAsync(DbCommand command, CommandExecutedEventData eventData, DbDataReader result, CancellationToken cancellationToken = default) { if (eventData.Duration.TotalMilliseconds >= GlobalContext.SystemConfig.DBSlowSqlLogTime * 1000) { LogHelper.Warn("耗时的Sql:" + command.GetCommandText()); } var reader = await base.ReaderExecutedAsync(command, eventData, result, cancellationToken); return reader; } } }