Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ILogger support for TryRefreshAsync in .NET Core package #273

Merged
merged 10 commits into from
Sep 8, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Extensions.Configuration.AzureAppConfiguration.Extensions;
using Microsoft.Extensions.Configuration.AzureAppConfiguration.FeatureManagement;
using Microsoft.Extensions.Configuration.AzureAppConfiguration.Models;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -176,17 +177,37 @@ public async Task RefreshAsync()
}
}

public async Task<bool> TryRefreshAsync()
public async Task<bool> TryRefreshAsync(ILogger logger)
avanigupta marked this conversation as resolved.
Show resolved Hide resolved
{
try
{
await RefreshAsync().ConfigureAwait(false);
}
catch (Exception e) when (
e is KeyVaultReferenceException ||
e is RequestFailedException ||
((e as AggregateException)?.InnerExceptions?.All(e => e is RequestFailedException) ?? false) ||
e is OperationCanceledException)
catch (RequestFailedException e)
{
if (e.Status == (int)HttpStatusCode.Unauthorized || e.Status == (int)HttpStatusCode.Forbidden)
avanigupta marked this conversation as resolved.
Show resolved Hide resolved
{
logger?.LogError(e, "Refresh operation failed due to an authentication error");
}
zhenlan marked this conversation as resolved.
Show resolved Hide resolved

return false;
}
catch (AggregateException e) when (e?.InnerExceptions?.All(e => e is RequestFailedException) ?? false)
{
if (e.InnerExceptions.Any(exception => (exception is RequestFailedException ex)
&& (ex.Status == (int)HttpStatusCode.Unauthorized || ex.Status == (int)HttpStatusCode.Forbidden)))
{
logger?.LogError(e, "Refresh operation failed due to an authentication error.");
}
zhenlan marked this conversation as resolved.
Show resolved Hide resolved

return false;
}
catch (KeyVaultReferenceException e)
{
logger?.LogError(e, "Refresh operation failed while resolving a Key Vault reference.");
return false;
}
catch (OperationCanceledException)
{
return false;
avanigupta marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;

Expand All @@ -24,14 +25,14 @@ public async Task RefreshAsync()
await _provider.RefreshAsync().ConfigureAwait(false);
}

public async Task<bool> TryRefreshAsync()
public async Task<bool> TryRefreshAsync(ILogger logger)
{
if (_provider == null)
{
return false;
}

return await _provider.TryRefreshAsync().ConfigureAwait(false);
return await _provider.TryRefreshAsync(logger).ConfigureAwait(false);
}

public void SetDirty(TimeSpan? maxDelay)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.
//
using Azure;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;

Expand Down Expand Up @@ -31,7 +32,8 @@ public interface IConfigurationRefresher
/// <summary>
/// Refreshes the data from App Configuration asynchronously. A return value indicates whether the operation succeeded.
/// </summary>
Task<bool> TryRefreshAsync();
/// <param name="logger">A logger for errors that might occur during refresh operation.</param>
Task<bool> TryRefreshAsync(ILogger logger = default);

/// <summary>
/// Sets the cached value for key-values registered for refresh as dirty.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.18" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.18" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.18" />
<PackageReference Include="System.Text.Json" Version="4.6.0" />
</ItemGroup>

Expand Down