Skip to content

Commit

Permalink
HttpClientHelper should not log when logging is disabled (#3150)
Browse files Browse the repository at this point in the history
  • Loading branch information
David R. Williamson authored Mar 9, 2023
1 parent d297180 commit 1a01e48
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions common/src/service/HttpClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,8 @@ private async Task ExecuteAsync(
IDictionary<HttpStatusCode, Func<HttpResponseMessage, Task<Exception>>> errorMappingOverrides,
CancellationToken cancellationToken)
{
Logging.Enter(this, httpMethod.Method, requestUri, nameof(ExecuteAsync));
if (Logging.IsEnabled)
Logging.Enter(this, httpMethod.Method, requestUri, nameof(ExecuteAsync));

try
{
Expand Down Expand Up @@ -780,7 +781,8 @@ private async Task ExecuteAsync(
}
catch (AggregateException ex)
{
Logging.Error(this, ex, nameof(ExecuteAsync));
if (Logging.IsEnabled)
Logging.Error(this, ex, nameof(ExecuteAsync));

ReadOnlyCollection<Exception> innerExceptions = ex.Flatten().InnerExceptions;
if (innerExceptions.Any(Fx.IsFatal))
Expand All @@ -799,25 +801,29 @@ private async Task ExecuteAsync(
}
catch (TimeoutException ex)
{
Logging.Error(this, ex, nameof(ExecuteAsync));
if (Logging.IsEnabled)
Logging.Error(this, ex, nameof(ExecuteAsync));

throw new IotHubCommunicationException(ex.Message, ex);
}
catch (IOException ex)
{
Logging.Error(this, ex, nameof(ExecuteAsync));
if (Logging.IsEnabled)
Logging.Error(this, ex, nameof(ExecuteAsync));

throw new IotHubCommunicationException(ex.Message, ex);
}
catch (HttpRequestException ex)
{
Logging.Error(this, ex, nameof(ExecuteAsync));
if (Logging.IsEnabled)
Logging.Error(this, ex, nameof(ExecuteAsync));

throw new IotHubCommunicationException(ex.Message, ex);
}
catch (TaskCanceledException ex)
{
Logging.Error(this, ex, nameof(ExecuteAsync));
if (Logging.IsEnabled)
Logging.Error(this, ex, nameof(ExecuteAsync));

// Unfortunately TaskCanceledException is thrown when HttpClient times out.
if (cancellationToken.IsCancellationRequested)
Expand All @@ -829,7 +835,8 @@ private async Task ExecuteAsync(
}
catch (Exception ex) when (!Fx.IsFatal(ex))
{
Logging.Error(this, ex, nameof(ExecuteAsync));
if (Logging.IsEnabled)
Logging.Error(this, ex, nameof(ExecuteAsync));

throw new IotHubException(ex.Message, ex);
}
Expand All @@ -842,7 +849,8 @@ private async Task ExecuteAsync(
}
finally
{
Logging.Exit(this, httpMethod.Method, requestUri, nameof(ExecuteAsync));
if (Logging.IsEnabled)
Logging.Exit(this, httpMethod.Method, requestUri, nameof(ExecuteAsync));
}
}

Expand Down

0 comments on commit 1a01e48

Please sign in to comment.