From a8530d7e0bb27b95d7ae3b7a6dced7e6f8929990 Mon Sep 17 00:00:00 2001 From: Marco Minerva Date: Tue, 24 Sep 2024 09:35:14 +0200 Subject: [PATCH] Use TryAdd for traceId in ProblemDetails.Extensions Modified the code to use the TryAdd method for adding the "traceId" to the ProblemDetails.Extensions dictionary. This change prevents potential overwrites of the "traceId" key, enhancing the robustness of the code by ensuring the key is only added if it does not already exist. --- .../Extensions/ServiceCollectionExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TinyHelpers.AspNetCore/Extensions/ServiceCollectionExtensions.cs b/src/TinyHelpers.AspNetCore/Extensions/ServiceCollectionExtensions.cs index b50f86c..784e532 100644 --- a/src/TinyHelpers.AspNetCore/Extensions/ServiceCollectionExtensions.cs +++ b/src/TinyHelpers.AspNetCore/Extensions/ServiceCollectionExtensions.cs @@ -73,7 +73,7 @@ public static IServiceCollection AddDefaultProblemDetails(this IServiceCollectio context.ProblemDetails.Type ??= $"https://httpstatuses.io/{statusCode}"; context.ProblemDetails.Title ??= ReasonPhrases.GetReasonPhrase(statusCode); context.ProblemDetails.Instance ??= context.HttpContext.Request.Path; - context.ProblemDetails.Extensions["traceId"] = Activity.Current?.Id ?? context.HttpContext.TraceIdentifier; + context.ProblemDetails.Extensions.TryAdd("traceId", Activity.Current?.Id ?? context.HttpContext.TraceIdentifier); }; });