Skip to content

Commit

Permalink
feat: Allow console output format overriding (#21)
Browse files Browse the repository at this point in the history
* chore(deps): update dependency serilog.sinks.opentelemetry to v4

* chore(deps): update all non-major dependencies

* feat: Allow console log output format overriding

chore(deps): update dependencies

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
  • Loading branch information
prom3theu5 and renovate[bot] authored Aug 6, 2024
1 parent b0d048c commit 4974a8d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
18 changes: 9 additions & 9 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
<Project>
<ItemGroup>
<PackageVersion Include="Aspirant.Hosting" Version="0.0.4" />
<PackageVersion Include="Aspire.Hosting.AppHost" Version="8.0.1" />
<PackageVersion Include="Aspire.Hosting.Seq" Version="8.0.2" />
<PackageVersion Include="Aspire.Seq" Version="8.0.2" />
<PackageVersion Include="Aspire.Hosting.AppHost" Version="8.1.0" />
<PackageVersion Include="Aspire.Hosting.Seq" Version="8.1.0" />
<PackageVersion Include="Aspire.Seq" Version="8.1.0" />
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="8.7.0" />
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery" Version="8.0.2" />
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery" Version="8.1.0" />
<PackageVersion Include="MinVer" Version="5.0.0" />
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
<PackageVersion Include="Serilog" Version="4.0.0" />
<PackageVersion Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageVersion Include="Serilog" Version="4.0.1" />
<PackageVersion Include="Serilog.AspNetCore" Version="8.0.2" />
<PackageVersion Include="Serilog.Enrichers.Context" Version="4.6.5" />
<PackageVersion Include="Serilog.Enrichers.Thread" Version="4.0.0" />
<PackageVersion Include="Serilog.Enrichers.Process" Version="3.0.0" />
<PackageVersion Include="Serilog.Enrichers.Span" Version="3.1.0" />
<PackageVersion Include="Serilog.Exceptions" Version="8.4.0" />
<PackageVersion Include="Serilog.Exceptions.EntityFrameworkCore" Version="8.4.0" />
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageVersion Include="Serilog.Sinks.OpenTelemetry" Version="3.0.0" />
<PackageVersion Include="Serilog.Sinks.OpenTelemetry" Version="4.0.0" />
<PackageVersion Include="Serilog.Sinks.Seq" Version="8.0.0" />
<PackageVersion Include="Aspire.Hosting.PostgreSQL" Version="8.0.2" />
<PackageVersion Include="Aspire.Npgsql" Version="8.0.1" />
<PackageVersion Include="Aspire.Hosting.PostgreSQL" Version="8.1.0" />
<PackageVersion Include="Aspire.Npgsql" Version="8.1.0" />
<PackageVersion Include="AspNetCore.HealthChecks.NpgSql" Version="8.0.1" />
<PackageVersion Include="AspNetCore.HealthChecks.Uris" Version="8.0.1" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
Expand Down
21 changes: 13 additions & 8 deletions src/SimCube.Aspire/Features/Otlp/OtlpServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ public static class OtlpServiceExtensions
{
private const string ConsoleOutputFormat = "[{Timestamp:HH:mm:ss}] | {Level:u4} | {SourceContext} | {Message:lj}{NewLine}{Exception}";

public static void AddOtlpServiceDefaults(this IHostApplicationBuilder builder)
public static void AddOtlpServiceDefaults(this IHostApplicationBuilder builder, string consoleOutputFormat = ConsoleOutputFormat)
{
builder.ConfigureSerilog();
if (string.IsNullOrEmpty(consoleOutputFormat))
{
consoleOutputFormat = ConsoleOutputFormat;
}

builder.ConfigureSerilog(consoleOutputFormat);

builder.ConfigureOpenTelemetry();

Expand All @@ -21,7 +26,7 @@ public static void AddOtlpServiceDefaults(this IHostApplicationBuilder builder)
});
}

public static LoggerConfiguration GetLoggerConfiguration(this IConfiguration configuration)
public static LoggerConfiguration GetLoggerConfiguration(this IConfiguration configuration, string consoleOutputFormat = ConsoleOutputFormat)
{
var config = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
Expand All @@ -35,11 +40,11 @@ public static LoggerConfiguration GetLoggerConfiguration(this IConfiguration con
.WithDefaultDestructurers()
.WithDestructurers([ new DbUpdateExceptionDestructurer() ]))
.Enrich.WithProperty(nameof(OtlpLiterals.ServiceName), configuration[OtlpLiterals.ServiceName])
.WriteTo.Spectre(outputTemplate: ConsoleOutputFormat);
.WriteTo.Spectre(outputTemplate: consoleOutputFormat);

if (!string.IsNullOrEmpty(configuration[OtlpLiterals.Endpoint]))
{
config.WriteTo.OpenTelemetry(options =>
config = config.WriteTo.OpenTelemetry(options =>
{
options.IncludedData = IncludedData.TraceIdField | IncludedData.SpanIdField;
options.Endpoint = configuration[OtlpLiterals.Endpoint];
Expand All @@ -51,7 +56,7 @@ public static LoggerConfiguration GetLoggerConfiguration(this IConfiguration con

if (!string.IsNullOrEmpty(configuration[SeqLiterals.SeqEndpoint]))
{
config.WriteTo.Seq(configuration[SeqLiterals.SeqEndpoint]);
config = config.WriteTo.Seq(configuration[SeqLiterals.SeqEndpoint]);
}

return config;
Expand All @@ -72,11 +77,11 @@ public static void MapDefaultEndpoints(this WebApplication app)
});
}

private static void ConfigureSerilog(this IHostApplicationBuilder builder)
private static void ConfigureSerilog(this IHostApplicationBuilder builder, string consoleOutputFormat)
{
ArgumentNullException.ThrowIfNull(builder);

builder.Services.AddSerilog(builder.Configuration.GetLoggerConfiguration().CreateLogger(), true);
builder.Services.AddSerilog(builder.Configuration.GetLoggerConfiguration(consoleOutputFormat).CreateLogger(), true);
}

private static void ConfigureOpenTelemetryLogging(this IHostApplicationBuilder builder) =>
Expand Down

0 comments on commit 4974a8d

Please sign in to comment.