Created
June 6, 2018 15:17
-
-
Save zippy1981/ddc56abad34ca2550908cd19c1a72d21 to your computer and use it in GitHub Desktop.
Example Application Insights RequestTelemetry enhancement.
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 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