refactor(AuthClient): Detaillierte Protokollierung für AuthClient hinzugefügt.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user