From 9429663ed3acecff6afa40a12250925887c73faa Mon Sep 17 00:00:00 2001 From: chlafreniere Date: Tue, 29 Mar 2022 18:32:01 -0700 Subject: [PATCH 1/2] First attempt SqlException Error Code --- src/Telemetry/Telemetry.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Telemetry/Telemetry.cs b/src/Telemetry/Telemetry.cs index 33eba4884..578cb6c25 100644 --- a/src/Telemetry/Telemetry.cs +++ b/src/Telemetry/Telemetry.cs @@ -10,6 +10,7 @@ using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.Extensions.Configuration; +using System.Globalization; namespace Microsoft.Azure.WebJobs.Extensions.Sql.Telemetry { @@ -26,6 +27,7 @@ public sealed class Telemetry private ILogger _logger; private bool _initialized; private const string InstrumentationKey = "98697a1c-1416-486a-99ac-c6c74ebe5ebd"; + private static readonly CultureInfo ENUSCultureInfo = new CultureInfo("en-US"); /// /// The environment variable used for opting out of telemetry /// @@ -120,6 +122,7 @@ public void TrackException(TelemetryErrorName errorName, Exception exception, ID this._logger.LogInformation($"Sending exception event: {exception.Message}"); properties ??= new Dictionary(); properties.Add(TelemetryPropertyName.ErrorName.ToString(), errorName.ToString()); + properties.Add(TelemetryPropertyName.ErrorCode.ToString(), ExtractErrorCode(exception)); //continue task in existing parallel thread this._trackEventTask = this._trackEventTask.ContinueWith( x => this.TrackExceptionTask(exception, properties, measurements) @@ -276,6 +279,19 @@ private Dictionary GetEventProperties(IDictionary @@ -330,7 +346,8 @@ public enum TelemetryPropertyName HasIdentityColumn, QueryType, ServerVersion, - Type + Type, + ErrorCode } /// From e5d07e447a83e851056262959f4a8a1e78d07f4b Mon Sep 17 00:00:00 2001 From: chlafreniere Date: Tue, 12 Apr 2022 13:11:45 -0700 Subject: [PATCH 2/2] Capture SqlException.Number --- src/Telemetry/Telemetry.cs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Telemetry/Telemetry.cs b/src/Telemetry/Telemetry.cs index 578cb6c25..51cd22579 100644 --- a/src/Telemetry/Telemetry.cs +++ b/src/Telemetry/Telemetry.cs @@ -11,6 +11,7 @@ using Microsoft.ApplicationInsights.Extensibility; using Microsoft.Extensions.Configuration; using System.Globalization; +using Microsoft.Data.SqlClient; namespace Microsoft.Azure.WebJobs.Extensions.Sql.Telemetry { @@ -27,7 +28,6 @@ public sealed class Telemetry private ILogger _logger; private bool _initialized; private const string InstrumentationKey = "98697a1c-1416-486a-99ac-c6c74ebe5ebd"; - private static readonly CultureInfo ENUSCultureInfo = new CultureInfo("en-US"); /// /// The environment variable used for opting out of telemetry /// @@ -280,15 +280,14 @@ private Dictionary GetEventProperties(IDictionary + /// Extract error code from known exception types + /// private static string ExtractErrorCode(Exception ex) { - if (ex == null) - { - return string.Empty; - } - if (ex is Data.SqlClient.SqlException) + if (ex != null && ex is SqlException) { - return (ex as Data.SqlClient.SqlException).ErrorCode.ToString(ENUSCultureInfo); + return (ex as SqlException).Number.ToString(CultureInfo.InvariantCulture); } return string.Empty; }