Skip to content

Commit

Permalink
Merge pull request #39 from configcat/log-wrapper-usage
Browse files Browse the repository at this point in the history
Use logger wrapper everywhere internally
  • Loading branch information
z4kn4fein authored Aug 11, 2022
2 parents 6dc6228 + 909daf0 commit 27563df
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 44 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
environment:
build_version: 6.5.2
build_version: 6.5.3
version: $(build_version)-{build}
image: Visual Studio 2022
configuration: Release
Expand Down
4 changes: 2 additions & 2 deletions src/ConfigCat.Client.Tests/ConfigCatClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ public void Dispose_ConfigServiceIsDisposable_ShouldInvokeDispose()
{
// Arrange

var myMock = new FakeConfigService(Mock.Of<IConfigFetcher>(), new CacheParameters(), Mock.Of<ILogger>());
var myMock = new FakeConfigService(Mock.Of<IConfigFetcher>(), new CacheParameters(), Mock.Of<ILogger>().AsWrapper());

IConfigCatClient instance = new ConfigCatClient(
myMock,
Expand Down Expand Up @@ -767,7 +767,7 @@ internal class FakeConfigService : ConfigServiceBase, IConfigService
{
public byte DisposeCount { get; private set; }

public FakeConfigService(IConfigFetcher configFetcher, CacheParameters cacheParameters, ILogger log) : base(configFetcher, cacheParameters, log)
public FakeConfigService(IConfigFetcher configFetcher, CacheParameters cacheParameters, LoggerWrapper log) : base(configFetcher, cacheParameters, log)
{
}

Expand Down
26 changes: 13 additions & 13 deletions src/ConfigCat.Client.Tests/ConfigServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public async Task LazyLoadConfigService_GetConfigAsync_ReturnsExpiredContent_Sho
using var service = new LazyLoadConfigService(
fetcherMock.Object,
new CacheParameters { ConfigCache = cacheMock.Object },
loggerMock.Object,
loggerMock.Object.AsWrapper(),
defaultExpire);

// Act
Expand Down Expand Up @@ -81,7 +81,7 @@ public async Task LazyLoadConfigService_GetConfigAsync_ReturnsNotExpiredContent_
using var service = new LazyLoadConfigService(
fetcherMock.Object,
new CacheParameters { ConfigCache = cacheMock.Object },
loggerMock.Object,
loggerMock.Object.AsWrapper(),
defaultExpire);

// Act
Expand Down Expand Up @@ -124,7 +124,7 @@ public async Task LazyLoadConfigService_RefreshConfigAsync_ShouldNotInvokeCacheG
using var service = new LazyLoadConfigService(
fetcherMock.Object,
new CacheParameters { ConfigCache = cacheMock.Object },
loggerMock.Object,
loggerMock.Object.AsWrapper(),
defaultExpire);

// Act
Expand Down Expand Up @@ -161,7 +161,7 @@ public async Task AutoPollConfigService_GetConfigAsync_WithoutTimerWithCachedCon
using var service = new AutoPollConfigService(config,
fetcherMock.Object,
new CacheParameters { ConfigCache = cacheMock.Object },
loggerMock.Object,
loggerMock.Object.AsWrapper(),
false);

// Act
Expand Down Expand Up @@ -199,7 +199,7 @@ public async Task AutoPollConfigService_GetConfigAsync_WithTimer_ShouldInvokeFet
using var service = new AutoPollConfigService(config,
fetcherMock.Object,
new CacheParameters { ConfigCache = cacheMock.Object },
loggerMock.Object,
loggerMock.Object.AsWrapper(),
true);

// Act
Expand Down Expand Up @@ -236,7 +236,7 @@ public async Task AutoPollConfigService_RefreshConfigAsync_ShouldOnceInvokeCache
using var service = new AutoPollConfigService(config,
fetcherMock.Object,
new CacheParameters { ConfigCache = cacheMock.Object },
loggerMock.Object,
loggerMock.Object.AsWrapper(),
false);

// Act
Expand Down Expand Up @@ -273,7 +273,7 @@ public async Task AutoPollConfigService_RefreshConfigAsync_ConfigChanged_ShouldR
using var service = new AutoPollConfigService(config,
fetcherMock.Object,
new CacheParameters { ConfigCache = cacheMock.Object },
loggerMock.Object,
loggerMock.Object.AsWrapper(),
false);

config.OnConfigurationChanged += (o, s) => { eventChanged++; };
Expand Down Expand Up @@ -308,7 +308,7 @@ public async Task AutoPollConfigService_Dispose_ShouldStopTimer()
using var service = new AutoPollConfigService(config,
fetcherMock.Object,
new CacheParameters { ConfigCache = cacheMock.Object, CacheKey = "" },
loggerMock.Object,
loggerMock.Object.AsWrapper(),
false);

// Act
Expand Down Expand Up @@ -345,7 +345,7 @@ public async Task AutoPollConfigService_WithoutTimer_InvokeDispose_ShouldDispose
using var service = new AutoPollConfigService(config,
fetcherMock.Object,
new CacheParameters { ConfigCache = cacheMock.Object },
loggerMock.Object,
loggerMock.Object.AsWrapper(),
false);

// Act
Expand All @@ -369,7 +369,7 @@ public async Task ManualPollConfigService_GetConfigAsync_ShouldInvokeCacheGet()
using var service = new ManualPollConfigService(
fetcherMock.Object,
new CacheParameters { ConfigCache = cacheMock.Object },
loggerMock.Object);
loggerMock.Object.AsWrapper());

// Act

Expand Down Expand Up @@ -409,7 +409,7 @@ public async Task ManualPollConfigService_RefreshConfigAsync_ShouldInvokeCacheGe
using var service = new ManualPollConfigService(
fetcherMock.Object,
new CacheParameters { ConfigCache = cacheMock.Object },
loggerMock.Object);
loggerMock.Object.AsWrapper());

// Act

Expand Down Expand Up @@ -440,7 +440,7 @@ public void ConfigService_InvokeDisposeManyTimes_ShouldInvokeFetcherDisposeExact
MockBehavior.Loose,
configFetcherMock.Object,
new CacheParameters { ConfigCache = new InMemoryConfigCache() },
loggerMock.Object)
loggerMock.Object.AsWrapper())
{
CallBase = true
};
Expand Down Expand Up @@ -471,7 +471,7 @@ public void ConfigService_WithNonDisposableConfigFetcher_DisposeShouldWork()
MockBehavior.Loose,
configFetcherMock.Object,
new CacheParameters { ConfigCache = new InMemoryConfigCache() },
new CounterLogger())
new CounterLogger().AsWrapper())
{
CallBase = true
};
Expand Down
4 changes: 2 additions & 2 deletions src/ConfigCat.Client.Tests/DataGovernanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public async Task WithDataGovernanceSetting_ShouldUseProperCdnUrl(DataGovernance
IConfigFetcher fetcher = new HttpConfigFetcher(
configuration.CreateUri(),
"DEMO",
Mock.Of<ILogger>(),
Mock.Of<ILogger>().AsWrapper(),
handlerMock.Object,
Mock.Of<IConfigDeserializer>(),
configuration.IsCustomBaseUrl,
Expand Down Expand Up @@ -377,7 +377,7 @@ internal static async Task<SortedList<byte, HttpRequestMessage>> Fetch(
IConfigFetcher fetcher = new HttpConfigFetcher(
fetchConfig.CreateUri(),
"DEMO",
Mock.Of<ILogger>(),
Mock.Of<ILogger>().AsWrapper(),
handlerMock.Object,
new ConfigDeserializer(),
fetchConfig.IsCustomBaseUrl,
Expand Down
10 changes: 10 additions & 0 deletions src/ConfigCat.Client.Tests/Fakes/LoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace ConfigCat.Client
{
internal static class LoggerExtensions
{
public static LoggerWrapper AsWrapper(this ILogger logger)
{
return new LoggerWrapper(logger);
}
}
}
8 changes: 4 additions & 4 deletions src/ConfigCat.Client.Tests/HttpConfigFetcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async Task HttpConfigFetcher_WithCustomHttpClientHandler_ShouldUsePassedH

var myHandler = new FakeHttpClientHandler();

var instance = new HttpConfigFetcher(new Uri("http://example.com"), "1.0", new CounterLogger(), myHandler, Mock.Of<IConfigDeserializer>(), false,
var instance = new HttpConfigFetcher(new Uri("http://example.com"), "1.0", new CounterLogger().AsWrapper(), myHandler, Mock.Of<IConfigDeserializer>(), false,
TimeSpan.FromSeconds(30));

// Act
Expand All @@ -35,7 +35,7 @@ public void HttpConfigFetcher_WithCustomHttpClientHandler_HandlersDisposeShouldN

var myHandler = new FakeHttpClientHandler();

var instance = new HttpConfigFetcher(new Uri("http://example.com"), "1.0", new CounterLogger(), myHandler, Mock.Of<IConfigDeserializer>(), false,
var instance = new HttpConfigFetcher(new Uri("http://example.com"), "1.0", new CounterLogger().AsWrapper(), myHandler, Mock.Of<IConfigDeserializer>(), false,
TimeSpan.FromSeconds(30));

// Act
Expand All @@ -54,7 +54,7 @@ public async Task HttpConfigFetcher_ResponseHttpCodeIsUnexpected_ShouldReturnsPa

var myHandler = new FakeHttpClientHandler(HttpStatusCode.Forbidden);

var instance = new HttpConfigFetcher(new Uri("http://example.com"), "1.0", new CounterLogger(), myHandler, Mock.Of<IConfigDeserializer>(), false,
var instance = new HttpConfigFetcher(new Uri("http://example.com"), "1.0", new CounterLogger().AsWrapper(), myHandler, Mock.Of<IConfigDeserializer>(), false,
TimeSpan.FromSeconds(30));

var lastConfig = new ProjectConfig("{ }", DateTime.UtcNow, "\"ETAG\"");
Expand All @@ -75,7 +75,7 @@ public async Task HttpConfigFetcher_ThrowAnException_ShouldReturnPassedConfig()

var myHandler = new ExceptionThrowerHttpClientHandler(new WebException());

var instance = new HttpConfigFetcher(new Uri("http://example.com"), "1.0", new CounterLogger(), myHandler, Mock.Of<IConfigDeserializer>(), false,
var instance = new HttpConfigFetcher(new Uri("http://example.com"), "1.0", new CounterLogger().AsWrapper(), myHandler, Mock.Of<IConfigDeserializer>(), false,
TimeSpan.FromSeconds(30));

var lastConfig = new ProjectConfig("{ }", DateTime.UtcNow, "\"ETAG\"");
Expand Down
19 changes: 10 additions & 9 deletions src/ConfigCatClient/ConfigCatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ConfigCat.Client
/// </summary>
public sealed class ConfigCatClient : IConfigCatClient
{
private readonly ILogger log;
private readonly LoggerWrapper log;
private readonly IRolloutEvaluator configEvaluator;
private readonly IConfigService configService;
private readonly IConfigDeserializer configDeserializer;
Expand Down Expand Up @@ -138,15 +138,16 @@ public ConfigCatClient(Action<ConfigCatClientOptions> configurationAction)
}

this.configService = this.overrideBehaviour == null || this.overrideBehaviour != OverrideBehaviour.LocalOnly
? DetermineConfigService(configuration,
? DetermineConfigService(configuration.PollingMode,
new HttpConfigFetcher(configuration.CreateUri(),
$"{configuration.PollingMode.Identifier}-{Version}",
configuration.Logger,
this.log,
configuration.HttpClientHandler,
this.configDeserializer,
configuration.IsCustomBaseUrl,
configuration.HttpTimeout),
cacheParameters)
cacheParameters,
this.log)
: new EmptyConfigService();
}

Expand Down Expand Up @@ -464,21 +465,21 @@ async Task<IDictionary<string, Setting>> GetRemoteConfigAsync()
}
}

private static IConfigService DetermineConfigService(ConfigCatClientOptions options, HttpConfigFetcher fetcher, CacheParameters cacheParameters)
private static IConfigService DetermineConfigService(PollingMode pollingMode, HttpConfigFetcher fetcher, CacheParameters cacheParameters, LoggerWrapper logger)
{
return options.PollingMode switch
return pollingMode switch
{
AutoPoll autoPoll => new AutoPollConfigService(autoPoll,
fetcher,
cacheParameters,
options.Logger),
logger),
LazyLoad lazyLoad => new LazyLoadConfigService(fetcher,
cacheParameters,
options.Logger,
logger,
lazyLoad.CacheTimeToLive),
ManualPoll => new ManualPollConfigService(fetcher,
cacheParameters,
options.Logger),
logger),
_ => throw new ArgumentException("Invalid configuration type."),
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/ConfigCatClient/ConfigService/AutoPollConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ internal AutoPollConfigService(
AutoPoll configuration,
IConfigFetcher configFetcher,
CacheParameters cacheParameters,
ILogger logger) : this(configuration, configFetcher, cacheParameters, logger, true)
LoggerWrapper logger) : this(configuration, configFetcher, cacheParameters, logger, true)
{ }

// For test purposes only
internal AutoPollConfigService(
AutoPoll configuration,
IConfigFetcher configFetcher,
CacheParameters cacheParameters,
ILogger logger,
LoggerWrapper logger,
bool startTimer
) : base(configFetcher, cacheParameters, logger)
{
Expand Down
4 changes: 2 additions & 2 deletions src/ConfigCatClient/ConfigService/ConfigServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ internal abstract class ConfigServiceBase : IDisposable
protected readonly IConfigCache ConfigCache; // Backward compatibility, it'll be changed to IConfigCatCache later.
#pragma warning restore CS0618 // Type or member is obsolete

protected readonly ILogger Log;
protected readonly LoggerWrapper Log;

protected readonly string CacheKey;

protected ConfigServiceBase(IConfigFetcher configFetcher, CacheParameters cacheParameters, ILogger log)
protected ConfigServiceBase(IConfigFetcher configFetcher, CacheParameters cacheParameters, LoggerWrapper log)
{
this.ConfigFetcher = configFetcher;
this.ConfigCache = cacheParameters.ConfigCache;
Expand Down
2 changes: 1 addition & 1 deletion src/ConfigCatClient/ConfigService/LazyLoadConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal sealed class LazyLoadConfigService : ConfigServiceBase, IConfigService
{
private readonly TimeSpan cacheTimeToLive;

internal LazyLoadConfigService(IConfigFetcher configFetcher, CacheParameters cacheParameters, ILogger logger, TimeSpan cacheTimeToLive)
internal LazyLoadConfigService(IConfigFetcher configFetcher, CacheParameters cacheParameters, LoggerWrapper logger, TimeSpan cacheTimeToLive)
: base(configFetcher, cacheParameters, logger)
{
this.cacheTimeToLive = cacheTimeToLive;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace ConfigCat.Client.ConfigService
{
internal sealed class ManualPollConfigService : ConfigServiceBase, IConfigService
{
internal ManualPollConfigService(IConfigFetcher configFetcher, CacheParameters cacheParameters, ILogger logger)
internal ManualPollConfigService(IConfigFetcher configFetcher, CacheParameters cacheParameters, LoggerWrapper logger)
: base(configFetcher, cacheParameters, logger) { }

public ProjectConfig GetConfig()
Expand Down
4 changes: 2 additions & 2 deletions src/ConfigCatClient/Evaluate/RolloutEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ namespace ConfigCat.Client.Evaluate
{
internal class RolloutEvaluator : IRolloutEvaluator
{
private readonly ILogger log;
private readonly LoggerWrapper log;

public RolloutEvaluator(ILogger logger)
public RolloutEvaluator(LoggerWrapper logger)
{
this.log = logger;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ConfigCatClient/HttpConfigFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal sealed class HttpConfigFetcher : IConfigFetcher, IDisposable
{
private readonly object lck = new();
private readonly string productVersion;
private readonly ILogger log;
private readonly LoggerWrapper log;

private readonly HttpClientHandler httpClientHandler;
private readonly IConfigDeserializer deserializer;
Expand All @@ -24,7 +24,7 @@ internal sealed class HttpConfigFetcher : IConfigFetcher, IDisposable

private Uri requestUri;

public HttpConfigFetcher(Uri requestUri, string productVersion, ILogger logger,
public HttpConfigFetcher(Uri requestUri, string productVersion, LoggerWrapper logger,
HttpClientHandler httpClientHandler, IConfigDeserializer deserializer, bool isCustomUri, TimeSpan timeout)
{
this.requestUri = requestUri;
Expand Down
2 changes: 1 addition & 1 deletion src/ConfigCatClient/Override/FlagOverrides.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private FlagOverrides(IDictionary<string, object> dictionary, OverrideBehaviour
/// </summary>
public OverrideBehaviour OverrideBehaviour { get; private set; }

internal IOverrideDataSource BuildDataSource(ILogger logger)
internal IOverrideDataSource BuildDataSource(LoggerWrapper logger)
{
if (this.dictionary != null)
return new LocalDictionaryDataSource(this.dictionary);
Expand Down
4 changes: 2 additions & 2 deletions src/ConfigCatClient/Override/LocalFileDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ internal sealed class LocalFileDataSource : IOverrideDataSource

private DateTime fileLastWriteTime;
private readonly string fullPath;
private readonly ILogger logger;
private readonly LoggerWrapper logger;
private readonly CancellationTokenSource cancellationTokenSource = new();

private volatile IDictionary<string, Setting> overrideValues;

public LocalFileDataSource(string filePath, bool autoReload, ILogger logger)
public LocalFileDataSource(string filePath, bool autoReload, LoggerWrapper logger)
{
if (!File.Exists(filePath))
{
Expand Down

0 comments on commit 27563df

Please sign in to comment.