This file contains hidden or 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 class AuditMiddleware | |
| { | |
| private readonly RequestDelegate _next; | |
| public AuditMiddleware(RequestDelegate next) => _next = next; | |
| public async Task InvokeAsync(HttpContext context) | |
| { | |
| var endpoint = context.GetEndpoint(); // <-- Enables benefit of the middleware improvement | |
| var requiresAudit = endpoint?.Metadata.GetMetadata<RequiresAuditAttribute>(); |
This file contains hidden or 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
| // This attribute encodes the exact position of the call site to intercept: | |
| // file path + line + column, hashed into a compact base64 string | |
| [InterceptsLocationAttribute(1, "qjmcoI/hUdYHdlM5/alrVYsBAABPcmRlclNlcnZpY2UuY3M=")] | |
| internal static void Log_Intercepted_0(string message, LogLevel level) | |
| { | |
| // Substitute logic — zero-allocation, AOT-friendly, audit trail, etc. | |
| StructuredLogger.Write(level, message); | |
| } |
This file contains hidden or 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
| // This is what ends up in the compiled IL — invisible to the developer | |
| GeneratedInterceptors.Log_Intercepted_0("Order created", LogLevel.Information); |
This file contains hidden or 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
| // This is what the developer writes | |
| Logger.Log("Order created", LogLevel.Information); |
This file contains hidden or 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
| // <auto-generated/> | |
| #nullable enable | |
| using System.Net.Http; | |
| using System.Threading; | |
| using System.Threading.Tasks; | |
| using MyApi; | |
| namespace System.Runtime.CompilerServices | |
| { | |
| [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] |
This file contains hidden or 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
| private static void Generate( | |
| SourceProductionContext spc, | |
| ImmutableArray<InterceptorTarget> targets) | |
| { | |
| if (targets.IsDefaultOrEmpty) return; | |
| var sb = new StringBuilder(); | |
| sb.AppendLine("// <auto-generated/>"); | |
| sb.AppendLine("#nullable enable"); | |
| sb.AppendLine("using System.Net.Http;"); |
This file contains hidden or 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.Collections.Immutable; | |
| using System.Text; | |
| using Microsoft.CodeAnalysis; | |
| using Microsoft.CodeAnalysis.CSharp; | |
| using Microsoft.CodeAnalysis.CSharp.Syntax; | |
| namespace CorrelationInterceptors.Generator; | |
| [Generator] | |
| public sealed class HttpClientInterceptorGenerator : IIncrementalGenerator |
This file contains hidden or 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
| namespace MyApi; | |
| public class OrderService(HttpClient httpClient) | |
| { | |
| public async Task<string> GetOrderAsync(Guid orderId) | |
| { | |
| var request = new HttpRequestMessage( | |
| HttpMethod.Get, $"https://orders-api/orders/{orderId}"); | |
| // No manual header injection — the interceptor handles it at compile time |
This file contains hidden or 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
| namespace MyApi; | |
| public class CorrelationMiddleware(RequestDelegate next) | |
| { | |
| public async Task InvokeAsync(HttpContext context) | |
| { | |
| CorrelationContext.CorrelationId = | |
| context.Request.Headers["X-Correlation-ID"].FirstOrDefault() | |
| ?? Guid.NewGuid().ToString(); |
This file contains hidden or 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
| namespace MyApi; | |
| public static class CorrelationContext | |
| { | |
| private static readonly AsyncLocal<string?> _correlationId = new(); | |
| public static string? CorrelationId | |
| { | |
| get => _correlationId.Value; | |
| set => _correlationId.Value = value; |