-
Notifications
You must be signed in to change notification settings - Fork 4
/
ConfigurationOverrides.cs
94 lines (85 loc) · 3.59 KB
/
ConfigurationOverrides.cs
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
89
90
91
92
93
94
using LeanCode.Components.Startup;
using Microsoft.Extensions.Configuration;
using Serilog.Events;
namespace LeanCode.IntegrationTestHelpers;
public class ConfigurationOverrides : IConfigurationSource
{
public const LogEventLevel MinimumLevelDefault = LogEventLevel.Warning;
public const bool EnableInternalLogsDefault = false;
public const string ConnectionStringBaseDefault = "SqlServer__ConnectionStringBase";
public const string ConnectionStringKeyDefault = "SqlServer:ConnectionString";
public const string InternalBaseKeyDefault = "InternalBase";
public const string PublicBaseKeyDefault = "PublicBase";
private readonly LogEventLevel minimumLevel;
private readonly bool enableInternalLogs;
private readonly string connectionStringBase;
private readonly string connectionStringKey;
private readonly string internalBaseKey;
private readonly string publicBaseKey;
public ConfigurationOverrides(
LogEventLevel? minimumLevel = null,
bool? enableInternalLogs = null,
string? connectionStringBase = null,
string? connectionStringKey = null,
string? internalBaseKey = null,
string? publicBaseKey = null
)
{
this.minimumLevel = minimumLevel ?? MinimumLevelDefault;
this.enableInternalLogs = enableInternalLogs ?? EnableInternalLogsDefault;
this.connectionStringBase = connectionStringBase ?? ConnectionStringBaseDefault;
this.connectionStringKey = connectionStringKey ?? ConnectionStringKeyDefault;
this.internalBaseKey = internalBaseKey ?? InternalBaseKeyDefault;
this.publicBaseKey = publicBaseKey ?? PublicBaseKeyDefault;
}
public IConfigurationProvider Build(IConfigurationBuilder builder)
{
return new Provider(
minimumLevel,
enableInternalLogs,
connectionStringBase,
connectionStringKey,
internalBaseKey,
publicBaseKey
);
}
private sealed class Provider : ConfigurationProvider
{
private readonly LogEventLevel minimumLevel;
private readonly bool enableInternalLogs;
private readonly string connectionStringBase;
private readonly string connectionStringKey;
private readonly string internalBaseKey;
private readonly string publicBaseKey;
public Provider(
LogEventLevel minimumLevel,
bool enableInternalLogs,
string connectionStringBase,
string connectionStringKey,
string internalBaseKey,
string publicBaseKey
)
{
this.minimumLevel = minimumLevel;
this.enableInternalLogs = enableInternalLogs;
this.connectionStringBase = connectionStringBase;
this.connectionStringKey = connectionStringKey;
this.internalBaseKey = internalBaseKey;
this.publicBaseKey = publicBaseKey;
}
public override void Load()
{
var dbName = $"integration_tests_{Guid.NewGuid():N}";
var rest = Environment.GetEnvironmentVariable(connectionStringBase);
var dbConnStr = $"Database={dbName};" + rest;
Data = new Dictionary<string, string?>
{
[connectionStringKey] = dbConnStr,
[internalBaseKey] = "http://localhost",
[publicBaseKey] = "http://localhost",
[IHostBuilderExtensions.EnableDetailedInternalLogsKey] = enableInternalLogs.ToString(),
[IHostBuilderExtensions.MinimumLogLevelKey] = minimumLevel.ToString(),
};
}
}
}