Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/SquidStd.Core/Data/Bootstrap/SquidStdOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ public sealed class SquidStdOptions
/// "Application" property. When null or empty, the entry assembly name is used.
/// </summary>
public string? AppName { get; set; }

/// <summary>
/// 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.
/// </summary>
public string? AppVersion { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<AssemblyInformationalVersionAttribute>()
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ILogEventSink>(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()
{
Expand Down
Loading