Skip to content

Instantly share code, notes, and snippets.

@jernejk
Created January 17, 2024 01:03
Show Gist options
  • Save jernejk/7a47bc95b961777b3c8fe695675b40d8 to your computer and use it in GitHub Desktop.
Save jernejk/7a47bc95b961777b3c8fe695675b40d8 to your computer and use it in GitHub Desktop.
Custom TagWithContext to extend EF Core TagWith with caller class name, method and tag/purpose (optional)
public static class TagWithExtensions
{
public static IQueryable<T> TagWithContext<T>(this IQueryable<T> queryable, string? message = "", [CallerFilePath] string callerFileName = "", [CallerMemberName] string callerName = "")
{
string logScopeName = GenerateLogScopeName(message, callerFileName, callerName);
return queryable.TagWith(logScopeName);
}
private static string GenerateLogScopeName(string? message = null, string callerFileName = "", string callerName = "")
{
if (!string.IsNullOrWhiteSpace(message))
{
message = "-" + message;
}
string className = Path.GetFileNameWithoutExtension(callerFileName);
return className + "-" + callerName + message;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment