Skip to content

Commit 6530f39

Browse files
authored
Capture SqlException Number (#225)
* First attempt SqlException Error Code * Capture SqlException.Number
1 parent 16d0118 commit 6530f39

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/Telemetry/Telemetry.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using Microsoft.ApplicationInsights;
1111
using Microsoft.ApplicationInsights.Extensibility;
1212
using Microsoft.Extensions.Configuration;
13+
using System.Globalization;
14+
using Microsoft.Data.SqlClient;
1315

1416
namespace Microsoft.Azure.WebJobs.Extensions.Sql.Telemetry
1517
{
@@ -120,6 +122,7 @@ public void TrackException(TelemetryErrorName errorName, Exception exception, ID
120122
this._logger.LogInformation($"Sending exception event: {exception.Message}");
121123
properties ??= new Dictionary<string, string>();
122124
properties.Add(TelemetryPropertyName.ErrorName.ToString(), errorName.ToString());
125+
properties.Add(TelemetryPropertyName.ErrorCode.ToString(), ExtractErrorCode(exception));
123126
//continue task in existing parallel thread
124127
this._trackEventTask = this._trackEventTask.ContinueWith(
125128
x => this.TrackExceptionTask(exception, properties, measurements)
@@ -276,6 +279,18 @@ private Dictionary<string, string> GetEventProperties(IDictionary<string, string
276279
return this._commonProperties;
277280
}
278281
}
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+
}
279294
}
280295

281296
/// <summary>
@@ -330,7 +345,8 @@ public enum TelemetryPropertyName
330345
HasIdentityColumn,
331346
QueryType,
332347
ServerVersion,
333-
Type
348+
Type,
349+
ErrorCode
334350
}
335351

336352
/// <summary>

0 commit comments

Comments
 (0)