Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit e337c16

Browse files
printing configuration values and their sources
1 parent dfe3c50 commit e337c16

2 files changed

Lines changed: 152 additions & 14 deletions

File tree

src/SqlStreamStore.Server/Program.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using System.Threading;
35
using System.Threading.Tasks;
46
using Microsoft.AspNetCore.Hosting;
@@ -23,7 +25,11 @@ public static async Task<int> Main(string[] args)
2325

2426
private Program(string[] args)
2527
{
26-
_configuration = new SqlStreamStoreServerConfiguration(Environment.GetEnvironmentVariables(), args);
28+
_configuration = new SqlStreamStoreServerConfiguration(
29+
Environment.GetEnvironmentVariables(),
30+
args);
31+
32+
Console.WriteLine(_configuration.ToString());
2733

2834
Log.Logger = new LoggerConfiguration()
2935
.MinimumLevel.Is(_configuration.LogLevel)
Lines changed: 145 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,198 @@
11
using System;
22
using System.Collections;
3+
using System.Collections.Generic;
34
using System.Linq;
5+
using System.Text;
46
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Configuration.EnvironmentVariables;
8+
using Microsoft.VisualBasic;
59
using Serilog.Events;
610

711
namespace SqlStreamStore.Server
812
{
913
internal class SqlStreamStoreServerConfiguration
1014
{
1115
private readonly IConfigurationRoot _configuration;
16+
private Dictionary<string, (string source, string value)> _values;
1217

1318
public bool UseCanonicalUris => _configuration.GetValue<bool>("use-canonical-uris");
1419
public LogEventLevel LogLevel => _configuration.GetValue("log-level", LogEventLevel.Information);
1520
public string ConnectionString => _configuration.GetValue<string>("connection-string");
1621
public string Schema => _configuration.GetValue<string>("schema");
1722
public string Provider => _configuration.GetValue<string>("provider");
1823

19-
public SqlStreamStoreServerConfiguration(IDictionary environment, string[] args)
24+
public SqlStreamStoreServerConfiguration(
25+
IDictionary environment,
26+
string[] args)
2027
{
21-
if(environment == null)
28+
if (environment == null)
2229
throw new ArgumentNullException(nameof(environment));
23-
if(args == null)
30+
if (args == null)
2431
throw new ArgumentNullException(nameof(args));
32+
33+
_values = new Dictionary<string, (string source, string value)>();
34+
35+
void Log(string logName, IDictionary<string, string> data)
36+
{
37+
foreach (var (key, value) in data)
38+
{
39+
_values[key] = (logName, value);
40+
Console.WriteLine($"{key} came from {logName}");
41+
}
42+
}
43+
2544
_configuration = new ConfigurationBuilder()
26-
.AddCommandLine(args)
27-
.Add(new UpperCasedEnvironmentVariablesConfigurationSource(environment))
45+
.Add(new CommandLineConfigurationSource(args, Log))
46+
.Add(new EnvironmentVariablesConfigurationSource(environment, Log))
2847
.Build();
2948
}
3049

31-
private class UpperCasedEnvironmentVariablesConfigurationSource : IConfigurationSource
50+
public override string ToString()
51+
{
52+
const string delimiter = " | ";
53+
54+
var column0Width = _values.Keys.Count > 0 ? _values.Keys.Max(x => x.Length) : 0;
55+
var column1Width = _values.Values.Count > 0 ?_values.Values.Max(_ => _.value.Length) : 0;
56+
var column2Witdth = _values.Values.Count > 0 ?_values.Values.Max(_ => _.source.Length) : 0;
57+
58+
StringBuilder Append(StringBuilder builder, string column0, string column1, string column2) =>
59+
builder
60+
.Append(column0.PadRight(column0Width, ' '))
61+
.Append(delimiter)
62+
.Append(column1.PadRight(column1Width, ' '))
63+
.Append(delimiter)
64+
.AppendLine(column2);
65+
66+
return _values.Keys.OrderBy(x => x).Aggregate(
67+
Append(new StringBuilder().AppendLine("SQL Stream Store Configuration:"), "Argument", "Value", "Source")
68+
.AppendLine(new string('-', column0Width + column1Width + column2Witdth)),
69+
(builder, key) => Append(
70+
builder,
71+
key,
72+
_values[key].value,
73+
_values[key].source)).ToString();
74+
}
75+
76+
private class CommandLineConfigurationSource : IConfigurationSource
77+
{
78+
private readonly IEnumerable<string> _args;
79+
private readonly Action<string, IDictionary<string, string>> _log;
80+
81+
public CommandLineConfigurationSource(
82+
IEnumerable<string> args,
83+
Action<string, IDictionary<string, string>> log)
84+
{
85+
if (args == null)
86+
{
87+
throw new ArgumentNullException(nameof(args));
88+
}
89+
90+
if (log == null)
91+
{
92+
throw new ArgumentNullException(nameof(log));
93+
}
94+
95+
_args = args;
96+
_log = log;
97+
}
98+
99+
public IConfigurationProvider Build(IConfigurationBuilder builder)
100+
=> new CommandLineConfigurationProvider(_args, _log);
101+
}
102+
103+
private class CommandLineConfigurationProvider
104+
: Microsoft.Extensions.Configuration.CommandLine.CommandLineConfigurationProvider
105+
{
106+
private readonly Action<string, IDictionary<string, string>> _log;
107+
108+
public CommandLineConfigurationProvider(IEnumerable<string> args,
109+
Action<string, IDictionary<string, string>> log,
110+
IDictionary<string, string> switchMappings = null) : base(args, switchMappings)
111+
{
112+
if (log == null)
113+
{
114+
throw new ArgumentNullException(nameof(log));
115+
}
116+
117+
_log = log;
118+
}
119+
120+
public override void Load()
121+
{
122+
base.Load();
123+
124+
_log(nameof(CommandLineConfigurationSource), Data);
125+
}
126+
}
127+
128+
private class EnvironmentVariablesConfigurationSource : IConfigurationSource
32129
{
33130
private readonly IDictionary _environment;
131+
private readonly Action<string, IDictionary<string, string>> _log;
34132
public string Prefix { get; set; } = "SQLSTREAMSTORE";
35133

36-
public UpperCasedEnvironmentVariablesConfigurationSource(IDictionary environment)
134+
public EnvironmentVariablesConfigurationSource(
135+
IDictionary environment,
136+
Action<string, IDictionary<string, string>> log)
37137
{
138+
if (log == null)
139+
{
140+
throw new ArgumentNullException(nameof(log));
141+
}
142+
143+
if (environment == null)
144+
{
145+
throw new ArgumentNullException(nameof(environment));
146+
}
147+
38148
_environment = environment;
149+
_log = log;
39150
}
40151

41152
public IConfigurationProvider Build(IConfigurationBuilder builder)
42-
=> new UpperCasedEnvironmentVariablesConfigurationProvider(Prefix, _environment);
153+
=> new EnvironmentVariablesConfigurationProvider(Prefix, _environment, _log);
43154
}
44155

45-
private class UpperCasedEnvironmentVariablesConfigurationProvider : ConfigurationProvider
156+
private class EnvironmentVariablesConfigurationProvider : ConfigurationProvider
46157
{
47158
private readonly IDictionary _environment;
159+
private readonly Action<string, IDictionary<string, string>> _log;
48160
private readonly string _prefix;
49161

50-
public UpperCasedEnvironmentVariablesConfigurationProvider(string prefix, IDictionary environment)
162+
public EnvironmentVariablesConfigurationProvider(
163+
string prefix,
164+
IDictionary environment,
165+
Action<string, IDictionary<string, string>> log)
51166
{
52-
_environment = environment;
167+
if (log == null)
168+
{
169+
throw new ArgumentNullException(nameof(log));
170+
}
171+
172+
if (environment == null)
173+
{
174+
throw new ArgumentNullException(nameof(environment));
175+
}
176+
53177
_prefix = $"{prefix}_";
178+
_environment = environment;
179+
_log = log;
54180
}
55181

56182
public override void Load()
57183
{
58184
Data = (from entry in _environment.OfType<DictionaryEntry>()
59185
let key = (string) entry.Key
60186
where key.StartsWith(_prefix)
61-
select new { key = key.Remove(0, _prefix.Length).Replace('_', '-').ToLowerInvariant(), value = (string) entry.Value })
187+
select new
188+
{
189+
key = key.Remove(0, _prefix.Length).Replace('_', '-').ToLowerInvariant(),
190+
value = (string) entry.Value
191+
})
62192
.ToDictionary(x => x.key, x => x.value);
193+
194+
_log(nameof(EnvironmentVariablesConfigurationSource), Data);
63195
}
64196
}
65197
}
66-
}
198+
}

0 commit comments

Comments
 (0)