Created
January 17, 2024 01:03
-
-
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)
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 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