Skip to content

Commit e86fa2a

Browse files
authored
add extra logging (Azure#918)
1 parent e179283 commit e86fa2a

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/SqlBindingUtilities.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,16 @@ public static async Task<ServerProperties> GetServerTelemetryProperties(SqlConne
386386
/// <param name="cmd">The SqlCommand being executed</param>
387387
/// <param name="logger">The logger</param>
388388
/// <param name="cancellationToken">The cancellation token to pass to the call</param>
389+
/// <param name="logCommand">Defaults to false and when set logs the command being executed</param>
389390
/// <returns>The result of the call</returns>
390-
public static async Task<object> ExecuteScalarAsyncWithLogging(this SqlCommand cmd, ILogger logger, CancellationToken cancellationToken)
391+
public static async Task<object> ExecuteScalarAsyncWithLogging(this SqlCommand cmd, ILogger logger, CancellationToken cancellationToken, bool logCommand = false)
391392
{
392393
try
393394
{
395+
if (logCommand)
396+
{
397+
logger.LogDebug($"Executing query={cmd.CommandText}");
398+
}
394399
return await cmd.ExecuteScalarAsync(cancellationToken);
395400
}
396401
catch (Exception e)
@@ -406,11 +411,16 @@ public static async Task<object> ExecuteScalarAsyncWithLogging(this SqlCommand c
406411
/// <param name="cmd">The SqlCommand being executed</param>
407412
/// <param name="logger">The logger</param>
408413
/// <param name="cancellationToken">The cancellation token to pass to the call</param>
414+
/// <param name="logCommand">Defaults to false and when set logs the command being executed</param>
409415
/// <returns>The result of the call</returns>
410-
public static async Task<int> ExecuteNonQueryAsyncWithLogging(this SqlCommand cmd, ILogger logger, CancellationToken cancellationToken)
416+
public static async Task<int> ExecuteNonQueryAsyncWithLogging(this SqlCommand cmd, ILogger logger, CancellationToken cancellationToken, bool logCommand = false)
411417
{
412418
try
413419
{
420+
if (logCommand)
421+
{
422+
logger.LogDebug($"Executing query={cmd.CommandText}");
423+
}
414424
return await cmd.ExecuteNonQueryAsync(cancellationToken);
415425
}
416426
catch (Exception e)
@@ -426,11 +436,16 @@ public static async Task<int> ExecuteNonQueryAsyncWithLogging(this SqlCommand cm
426436
/// <param name="cmd">The SqlCommand being executed</param>
427437
/// <param name="logger">The logger</param>
428438
/// <param name="cancellationToken">The cancellation token to pass to the call</param>
439+
/// <param name="logCommand">Defaults to false and when set logs the command being executed</param>
429440
/// <returns>The result of the call</returns>
430-
public static async Task<SqlDataReader> ExecuteReaderAsyncWithLogging(this SqlCommand cmd, ILogger logger, CancellationToken cancellationToken)
441+
public static async Task<SqlDataReader> ExecuteReaderAsyncWithLogging(this SqlCommand cmd, ILogger logger, CancellationToken cancellationToken, bool logCommand = false)
431442
{
432443
try
433444
{
445+
if (logCommand)
446+
{
447+
logger.LogDebug($"Executing query={cmd.CommandText}");
448+
}
434449
return await cmd.ExecuteReaderAsync(cancellationToken);
435450
}
436451
catch (Exception e)

src/TriggerBinding/SqlTableChangeMonitor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ private async Task GetTableChangesAsync(SqlConnection connection, CancellationTo
284284
using (SqlCommand updateTablesPreInvocationCommand = this.BuildUpdateTablesPreInvocation(connection, transaction))
285285
{
286286
var commandSw = Stopwatch.StartNew();
287-
await updateTablesPreInvocationCommand.ExecuteNonQueryAsyncWithLogging(this._logger, token);
287+
await updateTablesPreInvocationCommand.ExecuteNonQueryAsyncWithLogging(this._logger, token, true);
288288
setLastSyncVersionDurationMs = commandSw.ElapsedMilliseconds;
289289
}
290290

@@ -510,7 +510,7 @@ private async Task RenewLeasesAsync(SqlConnection connection, CancellationToken
510510
{
511511
var stopwatch = Stopwatch.StartNew();
512512

513-
int rowsAffected = await renewLeasesCommand.ExecuteNonQueryAsyncWithLogging(this._logger, token);
513+
int rowsAffected = await renewLeasesCommand.ExecuteNonQueryAsyncWithLogging(this._logger, token, true);
514514

515515
long durationMs = stopwatch.ElapsedMilliseconds;
516516

@@ -613,7 +613,7 @@ private async Task ReleaseLeasesAsync(SqlConnection connection, CancellationToke
613613
using (SqlCommand releaseLeasesCommand = this.BuildReleaseLeasesCommand(connection, transaction))
614614
{
615615
var commandSw = Stopwatch.StartNew();
616-
int rowsUpdated = await releaseLeasesCommand.ExecuteNonQueryAsyncWithLogging(this._logger, token);
616+
int rowsUpdated = await releaseLeasesCommand.ExecuteNonQueryAsyncWithLogging(this._logger, token, true);
617617
releaseLeasesDurationMs = commandSw.ElapsedMilliseconds;
618618
}
619619

src/TriggerBinding/SqlTriggerMetricsProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private async Task<long> GetUnprocessedChangeCountAsync()
6666
using (SqlCommand getUnprocessedChangesCommand = this.BuildGetUnprocessedChangesCommand(connection, transaction, primaryKeyColumns, userTableId))
6767
{
6868
var commandSw = Stopwatch.StartNew();
69-
unprocessedChangeCount = (long)await getUnprocessedChangesCommand.ExecuteScalarAsyncWithLogging(this._logger, CancellationToken.None);
69+
unprocessedChangeCount = (long)await getUnprocessedChangesCommand.ExecuteScalarAsyncWithLogging(this._logger, CancellationToken.None, true);
7070
getUnprocessedChangesDurationMs = commandSw.ElapsedMilliseconds;
7171
}
7272

0 commit comments

Comments
 (0)