Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Created February 10, 2026 22:36
Show Gist options
  • Select an option

  • Save karenpayneoregon/f77381c084b6a563c6f81f61341158ee to your computer and use it in GitHub Desktop.

Select an option

Save karenpayneoregon/f77381c084b6a563c6f81f61341158ee to your computer and use it in GitHub Desktop.
EF Core extension to assist with debugging during development phase of a project

Sample usage

public static void DisplayTop5Customers()
{
    using var context = new Context();
    var customers = context.Customers
        .IgnoreQueryFilters()
        .TagWithDebugInfo("With ignore filter")
        .AsNoTracking()
        .Take(5)
        .ToList();
}

Log entry

info: 2/10/2026 14:22:39.311 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) 
      Executed DbCommand (4ms) [Parameters=[@__p_0='5'], CommandType='Text', CommandTimeout='30']
      -- Executing method DisplayTop5Customers in CustomerOperations.cs at line 41 message DisplayTop5Customers
      
      SELECT "c"."CustomerIdentifier", "c"."City", "c"."CompanyName", "c"."ContactId", "c"."ContactTypeIdentifier", "c"."CountryIdentifier", "c"."ModifiedDate", "c"."Phone", "c"."PostalCode", "c"."Street"
      FROM "Customers" AS "c"
      LIMIT @__p_0
using System.Runtime.CompilerServices;
using Microsoft.EntityFrameworkCore;
namespace TODO.Classes.Extensions;
/// <summary>
/// Provides extension methods for enhancing and debugging Entity Framework Core queries.
/// </summary>
public static class QueryExtensions
{
/// <summary>
/// Tags the provided query with debug information, including the calling member name,
/// file path, and line number, to assist in debugging during development.
/// </summary>
/// <typeparam name="T">The type of the elements in the query.</typeparam>
/// <param name="query">The query to be tagged with debug information.</param>
/// <param name="message">An optional debug message to include in the tag.</param>
/// <param name="memberName">The name of the calling member. Automatically provided by the compiler.</param>
/// <param name="filePath">The file path of the calling code. Automatically provided by the compiler.</param>
/// <param name="lineNumber">The line number of the calling code. Automatically provided by the compiler.</param>
/// <returns>The query tagged with debug information if in debug mode; otherwise, the original query.</returns>
/// <remarks>
/// This method is only effective in debug mode and is intended to provide additional context
/// for debugging purposes. In release mode, the query remains unaltered.
/// </remarks>
public static IQueryable<T> TagWithDebugInfo<T>(this IQueryable<T> query,
string message = "",
[CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "",
[CallerLineNumber] int lineNumber = 0)
{
#if DEBUG
var path = Path.GetFileName(filePath);
return query.TagWith(string.IsNullOrWhiteSpace(message) ?
$"Executing method {memberName} in {path} at line {lineNumber}" :
$"Executing method {memberName} in {path} at line {lineNumber} message {message}");
#else
return query;
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment