Skip to content

Add back command logging on some methods #918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/SqlBindingUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,16 @@ public static async Task<ServerProperties> GetServerTelemetryProperties(SqlConne
/// <param name="cmd">The SqlCommand being executed</param>
/// <param name="logger">The logger</param>
/// <param name="cancellationToken">The cancellation token to pass to the call</param>
/// <param name="logCommand">Defaults to false and when set logs the command being executed</param>
/// <returns>The result of the call</returns>
public static async Task<object> ExecuteScalarAsyncWithLogging(this SqlCommand cmd, ILogger logger, CancellationToken cancellationToken)
public static async Task<object> ExecuteScalarAsyncWithLogging(this SqlCommand cmd, ILogger logger, CancellationToken cancellationToken, bool logCommand = false)
{
try
{
if (logCommand)
{
logger.LogDebug($"Executing query={cmd.CommandText}");
}
return await cmd.ExecuteScalarAsync(cancellationToken);
}
catch (Exception e)
Expand All @@ -406,11 +411,16 @@ public static async Task<object> ExecuteScalarAsyncWithLogging(this SqlCommand c
/// <param name="cmd">The SqlCommand being executed</param>
/// <param name="logger">The logger</param>
/// <param name="cancellationToken">The cancellation token to pass to the call</param>
/// <param name="logCommand">Defaults to false and when set logs the command being executed</param>
/// <returns>The result of the call</returns>
public static async Task<int> ExecuteNonQueryAsyncWithLogging(this SqlCommand cmd, ILogger logger, CancellationToken cancellationToken)
public static async Task<int> ExecuteNonQueryAsyncWithLogging(this SqlCommand cmd, ILogger logger, CancellationToken cancellationToken, bool logCommand = false)
{
try
{
if (logCommand)
{
logger.LogDebug($"Executing query={cmd.CommandText}");
}
return await cmd.ExecuteNonQueryAsync(cancellationToken);
}
catch (Exception e)
Expand All @@ -426,11 +436,16 @@ public static async Task<int> ExecuteNonQueryAsyncWithLogging(this SqlCommand cm
/// <param name="cmd">The SqlCommand being executed</param>
/// <param name="logger">The logger</param>
/// <param name="cancellationToken">The cancellation token to pass to the call</param>
/// <param name="logCommand">Defaults to false and when set logs the command being executed</param>
/// <returns>The result of the call</returns>
public static async Task<SqlDataReader> ExecuteReaderAsyncWithLogging(this SqlCommand cmd, ILogger logger, CancellationToken cancellationToken)
public static async Task<SqlDataReader> ExecuteReaderAsyncWithLogging(this SqlCommand cmd, ILogger logger, CancellationToken cancellationToken, bool logCommand = false)
{
try
{
if (logCommand)
{
logger.LogDebug($"Executing query={cmd.CommandText}");
}
return await cmd.ExecuteReaderAsync(cancellationToken);
}
catch (Exception e)
Expand Down
6 changes: 3 additions & 3 deletions src/TriggerBinding/SqlTableChangeMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ private async Task GetTableChangesAsync(SqlConnection connection, CancellationTo
using (SqlCommand updateTablesPreInvocationCommand = this.BuildUpdateTablesPreInvocation(connection, transaction))
{
var commandSw = Stopwatch.StartNew();
await updateTablesPreInvocationCommand.ExecuteNonQueryAsyncWithLogging(this._logger, token);
await updateTablesPreInvocationCommand.ExecuteNonQueryAsyncWithLogging(this._logger, token, true);
setLastSyncVersionDurationMs = commandSw.ElapsedMilliseconds;
}

Expand Down Expand Up @@ -510,7 +510,7 @@ private async Task RenewLeasesAsync(SqlConnection connection, CancellationToken
{
var stopwatch = Stopwatch.StartNew();

int rowsAffected = await renewLeasesCommand.ExecuteNonQueryAsyncWithLogging(this._logger, token);
int rowsAffected = await renewLeasesCommand.ExecuteNonQueryAsyncWithLogging(this._logger, token, true);

long durationMs = stopwatch.ElapsedMilliseconds;

Expand Down Expand Up @@ -613,7 +613,7 @@ private async Task ReleaseLeasesAsync(SqlConnection connection, CancellationToke
using (SqlCommand releaseLeasesCommand = this.BuildReleaseLeasesCommand(connection, transaction))
{
var commandSw = Stopwatch.StartNew();
int rowsUpdated = await releaseLeasesCommand.ExecuteNonQueryAsyncWithLogging(this._logger, token);
int rowsUpdated = await releaseLeasesCommand.ExecuteNonQueryAsyncWithLogging(this._logger, token, true);
releaseLeasesDurationMs = commandSw.ElapsedMilliseconds;
}

Expand Down
2 changes: 1 addition & 1 deletion src/TriggerBinding/SqlTriggerMetricsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private async Task<long> GetUnprocessedChangeCountAsync()
using (SqlCommand getUnprocessedChangesCommand = this.BuildGetUnprocessedChangesCommand(connection, transaction, primaryKeyColumns, userTableId))
{
var commandSw = Stopwatch.StartNew();
unprocessedChangeCount = (long)await getUnprocessedChangesCommand.ExecuteScalarAsyncWithLogging(this._logger, CancellationToken.None);
unprocessedChangeCount = (long)await getUnprocessedChangesCommand.ExecuteScalarAsyncWithLogging(this._logger, CancellationToken.None, true);
getUnprocessedChangesDurationMs = commandSw.ElapsedMilliseconds;
}

Expand Down