refactor(AuthClient): Detaillierte Protokollierung für AuthClient hinzugefügt.

This commit is contained in:
Developer 02
2025-03-12 10:47:15 +01:00
parent 5f9efa3bb0
commit 031f830b8f
2 changed files with 27 additions and 11 deletions

View File

@@ -28,7 +28,19 @@ public class AuthClient : IAuthClient, IHostedService
_connection.On<string, string, string>(nameof(ReceivePublicKeyAsync), ReceivePublicKeyAsync);
_connection.Reconnected += async cnnId => await GetAllPublicKeysAsync();
_connection.Reconnected += async cnnId =>
{
_logger?.LogInformation("Auth-client reconnected. Number of connection attempts {nOfAttempts}.", _nOfAttempts);
await GetAllPublicKeysAsync();
_nOfAttempts = 0;
};
_connection.Reconnecting += (ex) =>
{
logger?.LogError(ex, "Auth-client disconnected. Attempt to reconnect every {time} seconds.", _params.RetryDelay!.Value.TotalSeconds);
_nOfAttempts += 1;
return Task.CompletedTask;
};
_logger = logger;
}
@@ -51,23 +63,27 @@ public class AuthClient : IAuthClient, IHostedService
await GetAllPublicKeysAsync();
}
private int nOfAttempts = 0;
private int _nOfAttempts = 0;
private async Task<bool> TryStartConnectionAsync(CancellationToken cancellationToken = default)
{
try
{
nOfAttempts += 1;
_nOfAttempts += 1;
await _connection.StartAsync(cancellationToken);
_logger?.LogInformation("Auth-client connection successful. Number of connection attempts {nOfAttempts}.", nOfAttempts);
_logger?.LogInformation("Auth-client connection successful. Number of connection attempts {nOfAttempts}.", _nOfAttempts);
_nOfAttempts = 0;
return true;
}
catch(HttpRequestException ex)
{
if (_params.RetryDelay is null)
_logger?.LogError(ex, "Auth-client connection failed. {message}", ex.Message);
else
_logger?.LogError(ex, "Auth-client connection failed and will be retried every {time} seconds. The status of being successful can be followed from the information logs.\n{message}", _params.RetryDelay.Value.TotalSeconds, ex.Message);
if(_nOfAttempts < 2)
{
if (_params.RetryDelay is null)
_logger?.LogError(ex, "Auth-client connection failed. {message}", ex.Message);
else
_logger?.LogError(ex, "Auth-client connection failed and will be retried every {time} seconds. The status of being successful can be followed from the information logs.\n{message}", _params.RetryDelay.Value.TotalSeconds, ex.Message);
}
return false;
}
}