Last active
June 7, 2024 14:16
-
-
Save tcartwright/30cd6673c53954c2493d9f7ca55350dc to your computer and use it in GitHub Desktop.
C#: EFCommandInterceptor - Intercepts EF commands and tags them with the class.method(parameters).linenumber. Even action queries, while .TagWithCallSite() only does Selects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Data.Common; | |
using System.Diagnostics; | |
using Microsoft.EntityFrameworkCore.Diagnostics; | |
namespace ~~~~~; | |
public class EFCommandInterceptor : DbCommandInterceptor | |
{ | |
public override InterceptionResult<int> NonQueryExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<int> result) | |
{ | |
var msg = GetLineInfo(); | |
FormatMessage(command, msg); | |
return base.NonQueryExecuting(command, eventData, result); | |
} | |
public override async ValueTask<InterceptionResult<int>> NonQueryExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<int> result, CancellationToken cancellationToken = default) | |
{ | |
var msg = GetLineInfo(); | |
FormatMessage(command, msg); | |
return await base.NonQueryExecutingAsync(command, eventData, result, cancellationToken); | |
} | |
public override InterceptionResult<DbDataReader> ReaderExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result) | |
{ | |
var msg = GetLineInfo(); | |
FormatMessage(command, msg); | |
return base.ReaderExecuting(command, eventData, result); | |
} | |
public override async ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result, CancellationToken cancellationToken = default) | |
{ | |
var msg = GetLineInfo(); | |
FormatMessage(command, msg); | |
return await base.ReaderExecutingAsync(command, eventData, result, cancellationToken); | |
} | |
public override InterceptionResult<object> ScalarExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<object> result) | |
{ | |
var msg = GetLineInfo(); | |
FormatMessage(command, msg); | |
return base.ScalarExecuting(command, eventData, result); | |
} | |
public override async ValueTask<InterceptionResult<object>> ScalarExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<object> result, CancellationToken cancellationToken = default) | |
{ | |
var msg = GetLineInfo(); | |
FormatMessage(command, msg); | |
return await base.ScalarExecutingAsync(command, eventData, result, cancellationToken); | |
} | |
protected string GetLineInfo(bool includeLineNumber = true) | |
{ | |
StackTrace stack = new StackTrace(true); | |
var frames = stack.GetFrames(); | |
var frame = frames | |
.Skip(2) | |
.SkipWhile(f => | |
{ | |
//var method = f.GetMethod(); | |
return f.GetFileName() is null; | |
}).FirstOrDefault(); | |
if (frame is null) { return string.Empty; } | |
var methodInfo = frame.GetMethod(); | |
var parameters = string.Join(", ", methodInfo.GetParameters().Select(p => $"{p.ParameterType.Name} {p.Name}")); | |
var lineNumber = includeLineNumber ? $" Line: {frame.GetFileLineNumber()}" : ""; | |
var message = $"{methodInfo.ReflectedType.FullName}.{methodInfo.Name}({parameters}){lineNumber}"; | |
Trace.WriteLine($"{this.GetType().Assembly.GetName().Name}: {message}"); | |
return message; | |
} | |
private static void FormatMessage(DbCommand command, string msg) | |
{ | |
if (string.IsNullOrWhiteSpace(msg)) { msg = "No Line Info"; } | |
command.CommandText = $"/*{msg}*/\r\n\r\n{command.CommandText}"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// in the program startup.cs: | |
services | |
.AddDbContextFactory<MyDbContext>(options => | |
options | |
.UseSqlServer(configuration.GetConnectionString(connectionStringName)) | |
.AddInterceptors(new EFCommandInterceptor()), | |
ServiceLifetime.Transient); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services | |
.AddDbContextFactory<MyDbContext>(options => | |
{ | |
options.UseSqlServer(configuration.GetConnectionString(defaultConnectionStringName)); | |
if (configuration.GetValue<bool>("Logging:LogEFCommands")) | |
{ | |
options.AddInterceptors(new EFCommandInterceptor()); | |
} | |
}, ServiceLifetime.Transient); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class MyDbContext : DbContext | |
{ | |
private readonly IDbCommandInterceptor _eFCommandInterceptor; | |
public MyDbContext(DbContextOptions<MyDbContext> options, IServiceProvider provider) : base(options) | |
{ | |
_eFCommandInterceptor = provider.GetService<IDbCommandInterceptor>(); | |
} | |
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |
{ | |
base.OnConfiguring(optionsBuilder); | |
if (_eFCommandInterceptor != null) | |
{ | |
optionsBuilder.AddInterceptors(_eFCommandInterceptor); | |
} | |
} | |
} | |
// Startup.cs: | |
if (configuration.GetValue<bool>("Logging:LogEFCommands")) | |
{ | |
services.AddScoped<IDbCommandInterceptor, EFCommandInterceptor>(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would be preferable to use .TagWithCallSite() as it is injected at compile time, while this interceptor is at run time and uses reflection. Which will cause a performance penalty. I have not measured how much as of yet.
Benefits:
Cons:
One thought to mitigate this performance cost is to add a configuration flag so that you can turn on and off the interceptor and use it only when needed.