Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zippy1981/ddc56abad34ca2550908cd19c1a72d21 to your computer and use it in GitHub Desktop.
Save zippy1981/ddc56abad34ca2550908cd19c1a72d21 to your computer and use it in GitHub Desktop.
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