From fa1b098f55acc81409b815f3fad8a57c8b9e10c7 Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Wed, 13 Sep 2023 08:54:59 +0100 Subject: [PATCH] Catch SqlExceptions --- .../ElasticApmModule.cs | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/integrations/Elastic.Apm.AspNetFullFramework/ElasticApmModule.cs b/src/integrations/Elastic.Apm.AspNetFullFramework/ElasticApmModule.cs index ad0e090b3..8d4c9bc3a 100644 --- a/src/integrations/Elastic.Apm.AspNetFullFramework/ElasticApmModule.cs +++ b/src/integrations/Elastic.Apm.AspNetFullFramework/ElasticApmModule.cs @@ -6,6 +6,7 @@ using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; +using System.Data.SqlClient; using System.Linq; using System.Security.Claims; using System.Threading; @@ -460,14 +461,21 @@ private void FillSampledTransactionContextUser(HttpContext context, ITransaction if (context.User is ClaimsPrincipal claimsPrincipal) { - static string GetClaimWithFallbackValue(ClaimsPrincipal principal, string claimType, string fallbackClaimType) + try { - var claim = principal.Claims.FirstOrDefault(n => n.Type == claimType || n.Type == fallbackClaimType); - return claim != null ? claim.Value : string.Empty; - } + static string GetClaimWithFallbackValue(ClaimsPrincipal principal, string claimType, string fallbackClaimType) + { + var claim = principal.Claims.FirstOrDefault(n => n.Type == claimType || n.Type == fallbackClaimType); + return claim != null ? claim.Value : string.Empty; + } - user.Email = GetClaimWithFallbackValue(claimsPrincipal, ClaimTypes.Email, OpenIdClaimTypes.Email); - user.Id = GetClaimWithFallbackValue(claimsPrincipal, ClaimTypes.NameIdentifier, OpenIdClaimTypes.UserId); + user.Email = GetClaimWithFallbackValue(claimsPrincipal, ClaimTypes.Email, OpenIdClaimTypes.Email); + user.Id = GetClaimWithFallbackValue(claimsPrincipal, ClaimTypes.NameIdentifier, OpenIdClaimTypes.UserId); + } + catch (SqlException ex) + { + _logger.Error()?.Log("Unable to access user claims due to SqlException with message: {message}", ex.Message); + } } transaction.Context.User = user;