fix(options): validate Host Node/Database/Logging options at startup — empty NodeName fails fast instead of NULLing SourceNode (plan R2-08 T7, arch-review 08r2 NF4)

This commit is contained in:
Joseph Doherty
2026-07-13 10:49:42 -04:00
parent f85f036b09
commit 4a0462e4d0
11 changed files with 224 additions and 3 deletions
@@ -0,0 +1,27 @@
using Serilog.Events;
using ZB.MOM.WW.Configuration;
namespace ZB.MOM.WW.ScadaBridge.Host;
/// <summary>
/// Validates <see cref="LoggingOptions"/> at host startup (arch-review 08 round
/// 2 NF4). <see cref="LoggingOptions.MinimumLevel"/> is parsed into a Serilog
/// <see cref="LogEventLevel"/> by <c>LoggerConfigurationFactory.ParseLevel</c>,
/// which silently falls back to <c>Information</c> for an unrecognised value.
/// This validator surfaces that misconfiguration at boot instead: a present,
/// non-blank value must name a real level (Verbose/Debug/Information/Warning/
/// Error/Fatal, case-insensitive). A null/blank value is treated as "unset"
/// (defaults to Information), mirroring the parser.
/// </summary>
public sealed class LoggingOptionsValidator : OptionsValidatorBase<LoggingOptions>
{
/// <inheritdoc />
protected override void Validate(ValidationBuilder builder, LoggingOptions options)
{
builder.RequireThat(
string.IsNullOrWhiteSpace(options.MinimumLevel)
|| Enum.TryParse<LogEventLevel>(options.MinimumLevel, ignoreCase: true, out _),
$"ScadaBridge:Logging:{nameof(LoggingOptions.MinimumLevel)} ('{options.MinimumLevel}') " +
$"is not a recognised Serilog level. Valid: {string.Join(", ", Enum.GetNames<LogEventLevel>())}.");
}
}