Skip to content

Commit bfa07cc

Browse files
authored
Merge pull request #49 from tgiachi/develop
release: AppVersion option
2 parents 64db7ec + 9782e49 commit bfa07cc

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

src/SquidStd.Core/Data/Bootstrap/SquidStdOptions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,11 @@ public sealed class SquidStdOptions
2020
/// "Application" property. When null or empty, the entry assembly name is used.
2121
/// </summary>
2222
public string? AppName { get; set; }
23+
24+
/// <summary>
25+
/// Gets or sets the application version used in the startup banner and as the Serilog
26+
/// "ApplicationVersion" property. When null or empty, the entry assembly informational
27+
/// version is used.
28+
/// </summary>
29+
public string? AppVersion { get; set; }
2330
}

src/SquidStd.Services.Core/Services/Bootstrap/SquidStdBootstrap.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public async ValueTask StartAsync(CancellationToken cancellationToken = default)
172172
logger.Information(
173173
"{Application:l} {ApplicationVersion:l} starting (SquidStd {SquidStdVersion:l}, config {ConfigName}, root {RootDirectory})",
174174
appName,
175-
ResolveVersion(Assembly.GetEntryAssembly()),
175+
ResolveAppVersion(Options),
176176
ResolveVersion(typeof(SquidStdBootstrap).Assembly),
177177
Options.ConfigName,
178178
Options.RootDirectory
@@ -317,6 +317,11 @@ private static string ResolveAppName(SquidStdOptions options)
317317
? options.AppName
318318
: Assembly.GetEntryAssembly()?.GetName().Name ?? "SquidStd";
319319

320+
private static string ResolveAppVersion(SquidStdOptions options)
321+
=> !string.IsNullOrWhiteSpace(options.AppVersion)
322+
? options.AppVersion
323+
: ResolveVersion(Assembly.GetEntryAssembly());
324+
320325
private static string ResolveVersion(Assembly? assembly)
321326
{
322327
var informational = assembly?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
@@ -344,7 +349,7 @@ private void ConfigureLogger()
344349

345350
loggerConfiguration
346351
.Enrich.WithProperty("Application", ResolveAppName(Options))
347-
.Enrich.WithProperty("ApplicationVersion", ResolveVersion(Assembly.GetEntryAssembly()));
352+
.Enrich.WithProperty("ApplicationVersion", ResolveAppVersion(Options));
348353

349354
if (options.MinimumLevel != LogLevelType.None)
350355
{

tests/SquidStd.Tests/Bootstrap/SquidStdBootstrapLifecycleLoggingTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,33 @@ public async Task ConfigureLogging_AttachesContainerSinksAndEnrichesEvents()
3535
Assert.True(probe.Properties.ContainsKey("ApplicationVersion"));
3636
}
3737

38+
[Fact]
39+
public async Task AppVersion_Override_IsUsedForEnrichment()
40+
{
41+
using var temp = new TempDirectory();
42+
var sink = new CapturingLogSink();
43+
await using var bootstrap = SquidStdBootstrap.Create(
44+
new SquidStdOptions
45+
{
46+
ConfigName = "app",
47+
RootDirectory = temp.Path,
48+
AppName = "MyApp",
49+
AppVersion = "9.9.9-test"
50+
}
51+
);
52+
bootstrap.ConfigureServices(c =>
53+
{
54+
c.RegisterInstance<ILogEventSink>(sink);
55+
return c;
56+
});
57+
58+
bootstrap.ConfigureLogging();
59+
Serilog.Log.Information("probe");
60+
61+
var probe = sink.Events.Single(e => e.MessageTemplate.Text == "probe");
62+
Assert.Equal("\"9.9.9-test\"", probe.Properties["ApplicationVersion"].ToString());
63+
}
64+
3865
[Fact]
3966
public async Task AppName_DefaultsToEntryAssemblyName()
4067
{

0 commit comments

Comments
 (0)