EfAbbreviationTagGenerator 0.0.2
dotnet add package EfAbbreviationTagGenerator --version 0.0.2
NuGet\Install-Package EfAbbreviationTagGenerator -Version 0.0.2
<PackageReference Include="EfAbbreviationTagGenerator" Version="0.0.2"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="EfAbbreviationTagGenerator" Version="0.0.2" />
<PackageReference Include="EfAbbreviationTagGenerator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add EfAbbreviationTagGenerator --version 0.0.2
#r "nuget: EfAbbreviationTagGenerator, 0.0.2"
#:package EfAbbreviationTagGenerator@0.0.2
#addin nuget:?package=EfAbbreviationTagGenerator&version=0.0.2
#tool nuget:?package=EfAbbreviationTagGenerator&version=0.0.2
EfAbbreviationTagGenerator
EfAbbreviationTagGenerator is a Roslyn Incremental Source Generator that enhances EF Core LINQ query tracing by generating deterministic and consistent SQL query tags based on the call site abbreviations.
🛠 Usage
- Reference this generator from your project (as an analyzer NuGet package or project reference).
- In your LINQ queries, simply call:
query.TagWithCallSiteAbbreviation();
- The source generator will automatically emit:
query.TagWith("#mcgu42");
The tag #mcgu42
is generated from a abbreviation of the call site like:
MyClass.GetUsers:L42
🔧 How It Works
- Scans all invocations of
TagWithCallSiteAbbreviation()
in your codebase. - Extracts:
- File path
- Method name
- Line number
- Computes an abbreviation.
- Emits a helper class:
internal static class AbbreviationTagExtensions
{
public static IQueryable<T> TagWithCallSiteAbbreviation<T>(
this IQueryable<T> query,
[CallerFilePath] string filePath = null,
[CallerMemberName] string memberName = null,
[CallerLineNumber] int lineNumber = 0)
{
var location = $"{Path.GetFileNameWithoutExtension(filePath)}.{memberName}:L{lineNumber}";
var hashTag = GetAbbreviationByLocation(location);
return query.TagWith(hashTag);
}
private static string GetAbbreviationByLocation(string location)
{
switch (location)
{
case "Test0.Main:L38": return "#tm38";
// ...
default: return location;
}
}
}
📝 Requirements
- EF Core (uses
.TagWith()
internally)
📂 Output Location
Add the following code to your project file to add the generated code to the project folder to be able to find the source code by abbreviation later:
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
And then exclude it from compilation
<ItemGroup>
<Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
</ItemGroup>
Useful links:
- Ef Hash Tag Generator
- Query tagging
- Better Tagging of EF Core Queries with .NET 6
- Automatic tagging with DbCommandInterceptor and StackTrace
- Slow Sql Server queries tagged with callsite hash
📃 License
Apache 2.0
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.