Skip to content

Commit

Permalink
[SQL] Adding logs to SqlRetryService (#4233)
Browse files Browse the repository at this point in the history
* Better logs in SqlRetryServicer

* Fix error in tests.
  • Loading branch information
fhibf authored Aug 20, 2024
1 parent c7af530 commit 8cef9b6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ public async Task ExecuteSql<TLogger>(Func<SqlConnection, CancellationToken, Sql
{
sqlException = sqlEx;
}

logger.LogInformation(ex, $"Attempt {retry}: {ex.Message}");
}

await Task.Delay(_retryMillisecondsDelay, cancellationToken);
Expand Down Expand Up @@ -474,6 +476,9 @@ public async Task<SqlConnection> GetConnection<TLogger>(ISqlConnectionBuilder sq
// Connection is never opened by the _sqlConnectionBuilder but RetryLogicProvider is set to the old, deprecated retry implementation. According to the .NET spec, RetryLogicProvider
// must be set before opening connection to take effect. Therefore we must reset it to null here before opening the connection.
conn.RetryLogicProvider = null; // To remove this line _sqlConnectionBuilder in healthcare-shared-components must be modified.
logger.LogInformation($"Retrieved {isReadOnlyConnection}connection to the database in {sw.Elapsed.TotalSeconds} seconds.");

sw = Stopwatch.StartNew();
await conn.OpenAsync(cancel);
logger.LogInformation($"Opened {isReadOnlyConnection}connection to the database in {sw.Elapsed.TotalSeconds} seconds.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,8 @@ await sqlRetryService.ExecuteSql(
CancellationToken.None);
}

Assert.Contains("Opened", logger.LogRecords[0].Message); // Check that logging of connection open was logged.
Assert.StartsWith("Retrieved", logger.LogRecords[0].Message); // Check that logging of connection open was logged.
Assert.StartsWith("Opened", logger.LogRecords[1].Message); // Check that logging of connection open was logged.
logger.LogRecords.RemoveAll(_ => _.Exception == null);
Assert.Single(logger.LogRecords);

Expand Down

0 comments on commit 8cef9b6

Please sign in to comment.