MS Application Insights integration for the Hellang.Middleware.ProblemDetails package
$ dotnet add package CcAcca.ApplicationInsights.ProblemDetailsMS Application Insights integration for the Hellang.Middleware.ProblemDetails package.
Enriches request telemetry with custom dimensions extracted from a ProblemDetail response:

Ensure your ASP.Net Core application has been configured with:
Install package
dotnet add package CcAcca.ApplicationInsights.ProblemDetails
Register the library with .Net Core dependency injection system. In Statup.ConfigureServices method add:
services.AddProblemDetailTelemetryInitializer()
Configure which ProblemDetail responses are sent to application insights:
services.AddProblemDetailTelemetryInitializer(o => {
o.ShouldSend = (ctx, problem) => problem.Status >= StatusCodes.Status500InternalServerError;
})
Configure which properties of an ProblemDetail should be sent to application insights:
services.AddProblemDetailTelemetryInitializer(o => {
o.IncludeErrorsValue = (ctx, problem) => !SensitiveUrl(ctx);
o.IncludeExtensionsValue = (ctx, problem) => !SensitiveUrl(ctx);
o.IncludeRawJson = (ctx, problem) => !SensitiveUrl(ctx);
});
Configure the values of the dimensions sent to application insights:
services.AddProblemDetailTelemetryInitializer(o => {
o.MapDimensions = (ctx, problem, dimensions) => {
var sensitiveFields = new[] {"SSN", "SocialSecurityNumber", "AccountNumber"};
var isSensitive = dimensions.Any(d => sensitiveFields.Any(f => d.Key.Contains(f)));
var sanitized = isSensitive
? dimensions
.Where(d =>
!sensitiveFields.Concat(new[] {DefaultDimensionCollector.RawDimensionKey})
.Any(f => d.Key.Contains(f)))
: dimensions;
return new Dictionary<string, string>(sanitized);
};
});
Configure when a ProblemDetail should be considered a success/failure
// only status codes >= 500 treat as a failure
services.AddProblemDetailTelemetryInitializer(options => {
options.IsFailure = ProblemDetailsTelemetryOptions.ServerErrorIsFailure;
});
Override the default serializer: see ProblemDetailsTelemetryOptions.SerializeValue
Override the default dimension collector for low level control: see DefaultDimensionCollector
To build and run tests you can use:
dotnet test src/CcAcca.ApplicationInsights.ProblemDetails.Tests)Github actions is used to run the dotnet cli tool to perform the build and test. See the yaml build definition for details.
Notes: