-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathBootstrapperLoggerFactoryTest.cs
More file actions
88 lines (72 loc) · 3.73 KB
/
BootstrapperLoggerFactoryTest.cs
File metadata and controls
88 lines (72 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Steeltoe.Common.TestResources;
namespace Steeltoe.Common.Logging.Test;
public sealed class BootstrapperLoggerFactoryTest
{
[Fact]
public async Task Upgrades_existing_loggers()
{
using var capturingLoggerProvider = new CapturingLoggerProvider(categoryName => categoryName.StartsWith("Test", StringComparison.Ordinal));
var bootstrapLoggerFactory = BootstrapLoggerFactory.CreateEmpty(loggingBuilder =>
{
loggingBuilder.SetMinimumLevel(LogLevel.Trace);
loggingBuilder.AddProvider(capturingLoggerProvider);
});
ILogger beforeStartLogger = bootstrapLoggerFactory.CreateLogger("Test_BeforeStart");
beforeStartLogger.LogTrace("Initializing (trace)");
WebApplicationBuilder builder = TestWebApplicationBuilderFactory.Create();
builder.Logging.ClearProviders();
builder.Logging.SetMinimumLevel(LogLevel.Warning);
builder.Logging.AddProvider(capturingLoggerProvider);
builder.Services.UpgradeBootstrapLoggerFactory(bootstrapLoggerFactory);
await using WebApplication app = builder.Build();
await app.StartAsync(TestContext.Current.CancellationToken);
var loggerFactory = app.Services.GetRequiredService<ILoggerFactory>();
ILogger afterStartLogger = loggerFactory.CreateLogger("Test_AfterStart");
afterStartLogger.LogTrace("Running (trace, ignored)");
afterStartLogger.LogWarning("Running (warning)");
beforeStartLogger.LogTrace("Running (trace, ignored)");
beforeStartLogger.LogWarning("Running (warning)");
IList<string> messages = capturingLoggerProvider.GetAll();
messages.Should().BeEquivalentTo([
"TRCE Test_BeforeStart: Initializing (trace)",
"WARN Test_AfterStart: Running (warning)",
"WARN Test_BeforeStart: Running (warning)"
], options => options.WithStrictOrdering());
}
[Fact]
public void Creates_default_minimum_levels()
{
var bootstrapLoggerFactory = BootstrapLoggerFactory.CreateConsole();
ILogger logger = bootstrapLoggerFactory.CreateLogger("TestLogger");
logger.IsEnabled(LogLevel.Trace).Should().BeFalse();
logger.IsEnabled(LogLevel.Debug).Should().BeFalse();
logger.IsEnabled(LogLevel.Information).Should().BeTrue();
logger.IsEnabled(LogLevel.Warning).Should().BeTrue();
logger.IsEnabled(LogLevel.Error).Should().BeTrue();
logger.IsEnabled(LogLevel.Critical).Should().BeTrue();
}
[Fact]
public void Can_override_minimum_level()
{
var appSettings = new Dictionary<string, string?>
{
["LogLevel:TestLogger"] = "Warning"
};
IConfigurationRoot configuration = new ConfigurationBuilder().AddInMemoryCollection(appSettings).Build();
var bootstrapLoggerFactory = BootstrapLoggerFactory.CreateConsole(loggingBuilder => loggingBuilder.AddConfiguration(configuration));
ILogger logger = bootstrapLoggerFactory.CreateLogger("TestLogger");
logger.IsEnabled(LogLevel.Trace).Should().BeFalse();
logger.IsEnabled(LogLevel.Debug).Should().BeFalse();
logger.IsEnabled(LogLevel.Information).Should().BeFalse();
logger.IsEnabled(LogLevel.Warning).Should().BeTrue();
logger.IsEnabled(LogLevel.Error).Should().BeTrue();
logger.IsEnabled(LogLevel.Critical).Should().BeTrue();
}
}