Skip to content

Commit

Permalink
add test for default logging output
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude committed Oct 7, 2024
1 parent ba70373 commit 76c57fc
Showing 1 changed file with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,45 @@ namespace commercetools.Api.IntegrationTests;

public class LoggingTest
{
[Fact]
public async void DefaultLogger()
{
var configuration = new ConfigurationBuilder().
AddJsonFile("appsettings.test.Development.json", true).
AddEnvironmentVariables().
AddUserSecrets<ServiceProviderFixture>().
AddEnvironmentVariables("CTP_").
Build();
var clientConfiguration = configuration.GetSection("Client").Get<ClientConfiguration>();
var loggerClientConf = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>()
{
{ "LoggerClient:ClientId", clientConfiguration.ClientId},
{ "LoggerClient:ClientSecret", clientConfiguration.ClientSecret},
{ "LoggerClient:ProjectKey", clientConfiguration.ProjectKey},
})
.Build();
var logger = new TestLogger();
var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddFilter("System.Net.Http.HttpClient", LogLevel.None) // disable HTTP client default logging
.AddProvider(new TestLoggerProvider(logger));
});
var s = new ServiceCollection();
s.AddSingleton(loggerFactory);
s.UseCommercetoolsApi(loggerClientConf, "LoggerClient");
var p = s.BuildServiceProvider();

var apiRoot = p.GetService<ProjectApiRoot>();

await apiRoot.Get().ExecuteAsync();

var messages = logger.GetLogMessages();
Assert.StartsWith("GET https://api.europe-west1.gcp.commercetools.com/" + clientConfiguration.ProjectKey, messages.TrimEnd());
}


[Fact]
public async void CustomLogger()
{
Expand Down Expand Up @@ -55,7 +94,7 @@ public async void CustomLogger()
var messages = logger.GetLogMessages();
Assert.Equal("GET https://api.europe-west1.gcp.commercetools.com/" + clientConfiguration.ProjectKey, messages.TrimEnd());
}

public class CustomLoggerHandler : DelegatingHandler
{
private readonly ILogger logger;
Expand Down

0 comments on commit 76c57fc

Please sign in to comment.