Skip to content

Commit 9c72e4a

Browse files
committed
feat: Better logging
1 parent 390ea62 commit 9c72e4a

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

Nickvision.Desktop/Keyring/KeyringService.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public KeyringService(ILogger<KeyringService> logger, AppInfo info, ISecretServi
3737
_credentials = [];
3838
_path = Path.Combine(keyringDir, $"{info.Id}.ring2");
3939
_connection = null;
40-
_logger.LogInformation($"Opening keyring database at {_path}...");
40+
_logger.LogInformation($"Opening keyring database ({_path}).");
4141
if (OperatingSystem.IsWindows() || OperatingSystem.IsMacOS() || OperatingSystem.IsLinux())
4242
{
4343
var secret = secretService.Get(info.Id) ?? secretService.Create(info.Id);
@@ -52,23 +52,23 @@ public KeyringService(ILogger<KeyringService> logger, AppInfo info, ISecretServi
5252
try
5353
{
5454
_connection.Open();
55-
_logger.LogInformation($"Opened keyring database at {_path} successfully.");
55+
_logger.LogInformation($"Opened keyring database ({_path}) successfully.");
5656
}
5757
catch (SqliteException e)
5858
{
59-
_logger.LogError($"Failed to open keyring database at {_path}: {e}");
59+
_logger.LogError($"Failed to open keyring database ({_path}): {e}");
6060
_connection.Dispose();
6161
_connection = null;
6262
}
6363
}
6464
else
6565
{
66-
_logger.LogWarning($"Unable to retrieve or create secret for {info.Id}. Keyring will not be saved to disk.");
66+
_logger.LogError($"Unable to open keyring database ({_path}). The system secret ({info.Id}) could not be retrieved or created.");
6767
}
6868
}
6969
if (_connection is null)
7070
{
71-
_logger.LogWarning($"Keyring database connection is unavailable. Changes will not be saved to disk.");
71+
_logger.LogError($"Keyring database ({_path}) connection is unavailable. Changes will not be saved to disk.");
7272
return;
7373
}
7474
using var createTableCommand = _connection.CreateCommand();
@@ -128,16 +128,16 @@ public void Dispose()
128128
/// <returns>True if the keyring was successfully added, else false</returns>
129129
public async Task<bool> AddCredentialAsync(Credential credential)
130130
{
131-
_logger.LogInformation($"Adding credential {credential.Name} to keyring...");
131+
_logger.LogInformation($"Adding keyring credential ({credential.Name}).");
132132
if (_credentials.Any(c => c.Name == credential.Name))
133133
{
134-
_logger.LogWarning($"Credential {credential.Name} already exists in keyring.");
134+
_logger.LogError($"Unable to add keyring credential ({credential.Name}) as it already exists.");
135135
return false;
136136
}
137137
_credentials.Add(credential);
138138
if (_connection is null)
139139
{
140-
_logger.LogWarning($"Keyring database connection is unavailable. Credential {credential.Name} will not be persisted to disk.");
140+
_logger.LogError($"Unable to persist keyring credential ({credential.Name}) to disk as the database connection is unavailable.");
141141
return false;
142142
}
143143
await using var insertCommand = _connection.CreateCommand();
@@ -149,11 +149,11 @@ public async Task<bool> AddCredentialAsync(Credential credential)
149149
var result = await insertCommand.ExecuteNonQueryAsync() > 0;
150150
if (result)
151151
{
152-
_logger.LogInformation($"Added credential {credential.Name} to keyring successfully.");
152+
_logger.LogInformation($"Added keyring credential ({credential.Name}) successfully.");
153153
}
154154
else
155155
{
156-
_logger.LogError($"Failed to persist credential {credential.Name} to keyring database.");
156+
_logger.LogError($"Failed to add keyring credential ({credential.Name}) to database.");
157157
}
158158
return result;
159159
}
@@ -164,18 +164,18 @@ public async Task<bool> AddCredentialAsync(Credential credential)
164164
/// <returns>True if the keyring was successfully added, else false</returns>
165165
public async Task<bool> DestroyAsync()
166166
{
167-
_logger.LogInformation($"Destroying keyring database at {_path}...");
167+
_logger.LogInformation($"Destroying keyring database ({_path}).");
168168
await DisposeAsync();
169169
_credentials.Clear();
170170
File.Delete(_path);
171171
var result = !File.Exists(_path);
172172
if (result)
173173
{
174-
_logger.LogInformation($"Destroyed keyring database at {_path} successfully.");
174+
_logger.LogInformation($"Destroyed keyring database ({_path}) successfully.");
175175
}
176176
else
177177
{
178-
_logger.LogError($"Failed to destroy keyring database at {_path}.");
178+
_logger.LogError($"Failed to destroy keyring database ({_path}).");
179179
}
180180
return result;
181181
}
@@ -187,17 +187,17 @@ public async Task<bool> DestroyAsync()
187187
/// <returns>True if the keyring was successfully removed, else false</returns>
188188
public async Task<bool> RemoveCredentialAsync(Credential credential)
189189
{
190-
_logger.LogInformation($"Removing credential {credential.Name} from keyring...");
190+
_logger.LogInformation($"Removing keyring credential ({credential.Name}).");
191191
var credentialIndex = _credentials.FindIndex(c => c.Name == credential.Name);
192192
if (credentialIndex == -1)
193193
{
194-
_logger.LogWarning($"Credential {credential.Name} not found in keyring.");
194+
_logger.LogError($"Unable to remove keyring credential ({credential.Name}) as it does not exist.");
195195
return false;
196196
}
197197
_credentials.RemoveAt(credentialIndex);
198198
if (_connection is null)
199199
{
200-
_logger.LogWarning($"Keyring database connection is unavailable. Credential {credential.Name} will not be removed from disk.");
200+
_logger.LogError($"Unable to remove keyring credential ({credential.Name}) from disk as the database connection is unavailable.");
201201
return false;
202202
}
203203
await using var deleteCommand = _connection.CreateCommand();
@@ -206,11 +206,11 @@ public async Task<bool> RemoveCredentialAsync(Credential credential)
206206
var result = await deleteCommand.ExecuteNonQueryAsync() > 0;
207207
if (result)
208208
{
209-
_logger.LogInformation($"Removed credential {credential.Name} from keyring successfully.");
209+
_logger.LogInformation($"Removed keyring credential ({credential.Name}) successfully.");
210210
}
211211
else
212212
{
213-
_logger.LogError($"Failed to remove credential {credential.Name} from keyring database.");
213+
_logger.LogError($"Failed to remove keyring credential ({credential.Name}) from database.");
214214
}
215215
return result;
216216
}
@@ -222,17 +222,17 @@ public async Task<bool> RemoveCredentialAsync(Credential credential)
222222
/// <returns>True if the keyring was successfully updated, else false</returns>
223223
public async Task<bool> UpdateCredentialAsync(Credential credential)
224224
{
225-
_logger.LogInformation($"Updating credential {credential.Name} in keyring...");
225+
_logger.LogInformation($"Updating keyring credential ({credential.Name}).");
226226
var credentialIndex = _credentials.FindIndex(c => c.Name == credential.Name);
227227
if (credentialIndex == -1)
228228
{
229-
_logger.LogWarning($"Credential {credential.Name} not found in keyring.");
229+
_logger.LogError($"Unable to update keyring credential ({credential.Name}) as it does not exist.");
230230
return false;
231231
}
232232
_credentials[credentialIndex] = credential;
233233
if (_connection is null)
234234
{
235-
_logger.LogWarning($"Keyring database connection is unavailable. Credential {credential.Name} will not be updated on disk.");
235+
_logger.LogError($"Unable to update keyring credential ({credential.Name}) on disk as the database connection is unavailable.");
236236
return false;
237237
}
238238
await using var updateCommand = _connection.CreateCommand();
@@ -244,11 +244,11 @@ public async Task<bool> UpdateCredentialAsync(Credential credential)
244244
var result = await updateCommand.ExecuteNonQueryAsync() > 0;
245245
if (result)
246246
{
247-
_logger.LogInformation($"Updated credential {credential.Name} in keyring successfully.");
247+
_logger.LogInformation($"Updated keyring credential ({credential.Name}) successfully.");
248248
}
249249
else
250250
{
251-
_logger.LogError($"Failed to update credential {credential.Name} in keyring database.");
251+
_logger.LogError($"Failed to update keyring credential ({credential.Name}) in database.");
252252
}
253253
return result;
254254
}

0 commit comments

Comments
 (0)