|
10 | 10 | using Microsoft.ApplicationInsights; |
11 | 11 | using Microsoft.ApplicationInsights.Extensibility; |
12 | 12 | using Microsoft.Extensions.Configuration; |
| 13 | +using System.Globalization; |
| 14 | +using Microsoft.Data.SqlClient; |
13 | 15 |
|
14 | 16 | namespace Microsoft.Azure.WebJobs.Extensions.Sql.Telemetry |
15 | 17 | { |
@@ -120,6 +122,7 @@ public void TrackException(TelemetryErrorName errorName, Exception exception, ID |
120 | 122 | this._logger.LogInformation($"Sending exception event: {exception.Message}"); |
121 | 123 | properties ??= new Dictionary<string, string>(); |
122 | 124 | properties.Add(TelemetryPropertyName.ErrorName.ToString(), errorName.ToString()); |
| 125 | + properties.Add(TelemetryPropertyName.ErrorCode.ToString(), ExtractErrorCode(exception)); |
123 | 126 | //continue task in existing parallel thread |
124 | 127 | this._trackEventTask = this._trackEventTask.ContinueWith( |
125 | 128 | x => this.TrackExceptionTask(exception, properties, measurements) |
@@ -276,6 +279,18 @@ private Dictionary<string, string> GetEventProperties(IDictionary<string, string |
276 | 279 | return this._commonProperties; |
277 | 280 | } |
278 | 281 | } |
| 282 | + |
| 283 | + /// <summary> |
| 284 | + /// Extract error code from known exception types |
| 285 | + /// </summary> |
| 286 | + private static string ExtractErrorCode(Exception ex) |
| 287 | + { |
| 288 | + if (ex != null && ex is SqlException) |
| 289 | + { |
| 290 | + return (ex as SqlException).Number.ToString(CultureInfo.InvariantCulture); |
| 291 | + } |
| 292 | + return string.Empty; |
| 293 | + } |
279 | 294 | } |
280 | 295 |
|
281 | 296 | /// <summary> |
@@ -330,7 +345,8 @@ public enum TelemetryPropertyName |
330 | 345 | HasIdentityColumn, |
331 | 346 | QueryType, |
332 | 347 | ServerVersion, |
333 | | - Type |
| 348 | + Type, |
| 349 | + ErrorCode |
334 | 350 | } |
335 | 351 |
|
336 | 352 | /// <summary> |
|
0 commit comments