Skip to content

Commit

Permalink
Merge pull request #14 from rquackenbush/feature/structured-logging
Browse files Browse the repository at this point in the history
Use structured logging
ggirard07 authored Oct 17, 2022
2 parents 97e49a1 + e4d6509 commit 8f85806
Showing 6 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -183,7 +183,7 @@ private void LogException(Exception ex, HttpContext context)
*/

// log exception to all registered targets
m_Logger.LogError(ex, $"Unhandled exception while executing the request {requestURL}");
m_Logger.LogError(ex, "Unhandled exception while executing the request {requestURL}", requestURL);
}

#endregion
6 changes: 3 additions & 3 deletions src/Distech.CloudRelay.API/Services/AzureIotHubAdapter.cs
Original file line number Diff line number Diff line change
@@ -110,13 +110,13 @@ private bool TryHandleIotHubException(string deviceId, IotHubException iotHubExc

// thrown when an attempt to communicate with the IoT Hub fails
case IotHubCommunicationException ex:
m_Logger.LogWarning($"An IotHubCommunicationException occurred: {ex}");
m_Logger.LogWarning(ex, $"An IotHubCommunicationException occurred");
apiException = new CommunicationException(ErrorCodes.CommunicationError, ex.Message, ex);
break;

// thrown when the IoT Hub returns an error code (i.e. device registration is disable which prevent the actual device from establishing a connection)
case ServerErrorException ex:
m_Logger.LogWarning($"A ServerErrorException occurred: {ex}");
m_Logger.LogWarning(ex, "A ServerErrorException occurred");
apiException = new CommunicationException(ErrorCodes.GatewayError, ErrorMessages.GetGatewayErrorMessage(), ex);
break;

@@ -133,7 +133,7 @@ private bool TryHandleIotHubException(string deviceId, IotHubException iotHubExc
// thrown when an error occurs during device client operation (i.e. device doesn't repond within the configured time out)
// shall always be kept last
case IotHubException ex:
m_Logger.LogWarning($"An IotHubException occurred: {ex}");
m_Logger.LogWarning(ex, "An IotHubException occurred");
apiException = new OperationException(ErrorCodes.DeviceOperationError, ErrorMessages.GetOperationErrorMessage(), ex);
break;

6 changes: 3 additions & 3 deletions src/Distech.CloudRelay.API/Services/DeviceService.cs
Original file line number Diff line number Diff line change
@@ -115,18 +115,18 @@ public async Task<DeviceResponse> InvokeRequestAsync(string deviceId, DeviceRequ
// verify whether a status code was received (throwing an exception here is likely to break previous integrations)
// 200 OK will be returned to the client if none of the response statuses are set
if (response.Status == null && response.Headers?.Status == null)
m_Logger.LogWarning($"Response status not set.");
m_Logger.LogWarning("Response status not set.");

return response;
}
catch(JsonReaderException ex)
{
m_Logger.LogWarning($"A JsonReaderException occurred: {ex}");
m_Logger.LogWarning(ex, "A JsonReaderException occurred");
throw new InvalidResultException(ErrorCodes.InvalidResult, ErrorMessages.GetInvalidResultMessage(), ex);
}
catch (JsonSerializationException ex)
{
m_Logger.LogWarning($"A JsonSerializationException occurred: {ex}");
m_Logger.LogWarning(ex, "A JsonSerializationException occurred");
throw new InvalidResultException(ErrorCodes.InvalidResult, ErrorMessages.GetInvalidResultMessage(), ex);
}
}
Original file line number Diff line number Diff line change
@@ -138,7 +138,7 @@ public async Task<BlobStreamDecorator> OpenBlobAsync(string blobPath)
{
if (ex.ErrorCode == BlobErrorCode.ContainerNotFound)
{
m_Logger.LogWarning($"Container '{blob.BlobContainerName}' does not exist");
m_Logger.LogWarning("Container '{BlobContainerName}' does not exist", blob.BlobContainerName);
}

throw new IdNotFoundException(ErrorCodes.BlobNotFound, blobPath);
6 changes: 3 additions & 3 deletions src/Distech.CloudRelay.Common/Services/FileService.cs
Original file line number Diff line number Diff line change
@@ -134,20 +134,20 @@ public async Task<int> CleanUpFilesAsync(uint minutesCleanupExpirationDelay)

//collect blobs for relay api -> device requests - any blobs in that path have been uploaded for the relay API purpose
blobs = await m_BlobRepository.ListBlobAsync(options.ServerFileUploadFolder);
m_Logger.LogDebug($"Found '{blobs.Count}' blob(s) for relay -> device requests");
m_Logger.LogDebug("Found '{blobCount}' blob(s) for relay -> device requests", blobs.Count);

//collect blobs for device -> relay api responses - filter blobs related to relay API context only
List<BlobInfo> responseBlobs = await m_BlobRepository.ListBlobAsync(options.DeviceFileUploadFolder);
List<BlobInfo> relayApiBlobs = responseBlobs.Where(b => b.Path.Contains(m_FileStorageOptions.Value.ServerFileUploadSubFolder)).ToList();
m_Logger.LogDebug($"Found '{relayApiBlobs.Count}' blob(s) for device -> relay responses");
m_Logger.LogDebug("Found '{relayApiBlobsCount}' blob(s) for device -> relay responses", relayApiBlobs.Count);
blobs.AddRange(relayApiBlobs);

//select expired blobs only
DateTime expirationDate = DateTime.UtcNow.AddMinutes(-minutesCleanupExpirationDelay);
Task<bool>[] deleteTasks = blobs.Where(b => b.LastModified < expirationDate)
.Select(b => m_BlobRepository.DeleteBlobAsync(b.Path))
.ToArray();
m_Logger.LogDebug($"Found '{deleteTasks.Count()}' expired blob(s)");
m_Logger.LogDebug("Found '{deleteTasksCount}' expired blob(s)", deleteTasks.Length);

//parrallel delete ends up degrading performances when dealing with large amount of blobs (longer latencies and throttling) and could also ends up reaching the host maximum number of outbound connections.
//the current implementation does not allow to control the amount of HttpClient created by the CloudBlobClient: https://github.com/Azure/azure-storage-net/issues/580
2 changes: 1 addition & 1 deletion src/Distech.CloudRelay.Functions/FileStorageFunctions.cs
Original file line number Diff line number Diff line change
@@ -56,7 +56,7 @@ public async Task CleanupExpiredBlobsAsync([TimerTrigger("0 0 0 * * sun")]TimerI
uint delay = m_Options.Value.MinutesExpirationDelay ?? DefaultMinutesExpirationDelay;
string expirationDate = DateTime.UtcNow.AddMinutes(-delay).ToString();
int deleted = await m_FileService.CleanUpFilesAsync(delay);
m_Logger.LogInformation($"Cleaned-up '{deleted}' expired blob(s) since {expirationDate}");
m_Logger.LogInformation("Cleaned-up '{deleted}' expired blob(s) since {expirationDate}", deleted, expirationDate);
}

#endregion

0 comments on commit 8f85806

Please sign in to comment.