Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example Application Insights RequestTelemetry enhancement.
public static IApplicationBuilder UseCorrelationProperties(this IApplicationBuilder app)
{
return app.Use(async (context, next) =>
{
var logger = context.RequestServices.GetRequiredService<ILogger<CorrelationProperties>>();
// NOTE: We're using Features and not RequestServices here.
var requestTelemetry = context.Features.Get<RequestTelemetry>();
if (requestTelemetry != null)
{
// These show up in the customDimensions field
requestTelemetry.Context.Properties["Key1"] = "Value1";
requestTelemetry.Context.Properties["Key2"] = "Value2";
}
else
{
logger.LogWarning(
$"Could not retrieve {typeof(RequestTelemetry).FullName} from {typeof(HttpContext).FullName}. This could mean Application Insights is not running");
}
await next.Invoke();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment