diff --git a/src/SquidStd.Core/Data/Bootstrap/SquidStdOptions.cs b/src/SquidStd.Core/Data/Bootstrap/SquidStdOptions.cs index ac97e72..5024eea 100644 --- a/src/SquidStd.Core/Data/Bootstrap/SquidStdOptions.cs +++ b/src/SquidStd.Core/Data/Bootstrap/SquidStdOptions.cs @@ -20,4 +20,11 @@ public sealed class SquidStdOptions /// "Application" property. When null or empty, the entry assembly name is used. /// public string? AppName { get; set; } + + /// + /// Gets or sets the application version used in the startup banner and as the Serilog + /// "ApplicationVersion" property. When null or empty, the entry assembly informational + /// version is used. + /// + public string? AppVersion { get; set; } } diff --git a/src/SquidStd.Services.Core/Services/Bootstrap/SquidStdBootstrap.cs b/src/SquidStd.Services.Core/Services/Bootstrap/SquidStdBootstrap.cs index fe534e0..a932295 100644 --- a/src/SquidStd.Services.Core/Services/Bootstrap/SquidStdBootstrap.cs +++ b/src/SquidStd.Services.Core/Services/Bootstrap/SquidStdBootstrap.cs @@ -172,7 +172,7 @@ public async ValueTask StartAsync(CancellationToken cancellationToken = default) logger.Information( "{Application:l} {ApplicationVersion:l} starting (SquidStd {SquidStdVersion:l}, config {ConfigName}, root {RootDirectory})", appName, - ResolveVersion(Assembly.GetEntryAssembly()), + ResolveAppVersion(Options), ResolveVersion(typeof(SquidStdBootstrap).Assembly), Options.ConfigName, Options.RootDirectory @@ -317,6 +317,11 @@ private static string ResolveAppName(SquidStdOptions options) ? options.AppName : Assembly.GetEntryAssembly()?.GetName().Name ?? "SquidStd"; + private static string ResolveAppVersion(SquidStdOptions options) + => !string.IsNullOrWhiteSpace(options.AppVersion) + ? options.AppVersion + : ResolveVersion(Assembly.GetEntryAssembly()); + private static string ResolveVersion(Assembly? assembly) { var informational = assembly?.GetCustomAttribute() @@ -344,7 +349,7 @@ private void ConfigureLogger() loggerConfiguration .Enrich.WithProperty("Application", ResolveAppName(Options)) - .Enrich.WithProperty("ApplicationVersion", ResolveVersion(Assembly.GetEntryAssembly())); + .Enrich.WithProperty("ApplicationVersion", ResolveAppVersion(Options)); if (options.MinimumLevel != LogLevelType.None) { diff --git a/tests/SquidStd.Tests/Bootstrap/SquidStdBootstrapLifecycleLoggingTests.cs b/tests/SquidStd.Tests/Bootstrap/SquidStdBootstrapLifecycleLoggingTests.cs index 4bbb0cb..a6eada9 100644 --- a/tests/SquidStd.Tests/Bootstrap/SquidStdBootstrapLifecycleLoggingTests.cs +++ b/tests/SquidStd.Tests/Bootstrap/SquidStdBootstrapLifecycleLoggingTests.cs @@ -35,6 +35,33 @@ public async Task ConfigureLogging_AttachesContainerSinksAndEnrichesEvents() Assert.True(probe.Properties.ContainsKey("ApplicationVersion")); } + [Fact] + public async Task AppVersion_Override_IsUsedForEnrichment() + { + using var temp = new TempDirectory(); + var sink = new CapturingLogSink(); + await using var bootstrap = SquidStdBootstrap.Create( + new SquidStdOptions + { + ConfigName = "app", + RootDirectory = temp.Path, + AppName = "MyApp", + AppVersion = "9.9.9-test" + } + ); + bootstrap.ConfigureServices(c => + { + c.RegisterInstance(sink); + return c; + }); + + bootstrap.ConfigureLogging(); + Serilog.Log.Information("probe"); + + var probe = sink.Events.Single(e => e.MessageTemplate.Text == "probe"); + Assert.Equal("\"9.9.9-test\"", probe.Properties["ApplicationVersion"].ToString()); + } + [Fact] public async Task AppName_DefaultsToEntryAssemblyName() {