Skip to content

Commit

Permalink
feat: add loki compatible logging output formatting (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
prom3theu5 authored Dec 19, 2024
1 parent 687e717 commit d32a3ed
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 48 deletions.
83 changes: 42 additions & 41 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
<Project>
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PackageVersion Include="Aspire.Hosting.AppHost" Version="8.2.2" />
<PackageVersion Include="Aspire.Hosting.Seq" Version="8.2.2" />
<PackageVersion Include="Aspire.Seq" Version="8.2.2" />
<PackageVersion Include="Aspire.Hosting.PostgreSQL" Version="8.2.2" />
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="8.10.0" />
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery" Version="8.2.2" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net9.0' ">
<PackageVersion Include="Aspire.Hosting.AppHost" Version="9.0.0" />
<PackageVersion Include="Aspire.Hosting.Seq" Version="9.0.0" />
<PackageVersion Include="Aspire.Seq" Version="9.0.0" />
<PackageVersion Include="Aspire.Hosting.PostgreSQL" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
<PackageVersion Include="MinVer" Version="6.0.0" />
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.10.0" />
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.10.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.1.0" />
<PackageVersion Include="Serilog.AspNetCore" Version="8.0.3" />
<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.Sinks.Console" Version="6.0.0" />
<PackageVersion Include="Serilog.Sinks.OpenTelemetry" Version="4.1.1" />
<PackageVersion Include="Serilog.Sinks.Seq" Version="8.0.0" />
<PackageVersion Include="Aspire.Npgsql" Version="8.1.0" />
<PackageVersion Include="AspNetCore.HealthChecks.NpgSql" Version="8.0.2" />
<PackageVersion Include="AspNetCore.HealthChecks.Uris" Version="8.0.1" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="Serilog.Sinks.Spectre" Version="0.5.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PackageVersion Include="Aspire.Hosting.AppHost" Version="8.2.2" />
<PackageVersion Include="Aspire.Hosting.Seq" Version="8.2.2" />
<PackageVersion Include="Aspire.Seq" Version="8.2.2" />
<PackageVersion Include="Aspire.Hosting.PostgreSQL" Version="8.2.2" />
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="8.10.0" />
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery" Version="8.2.2" />
<PackageVersion Include="AspNetCore.HealthChecks.NpgSql" Version="8.0.2" />
<PackageVersion Include="AspNetCore.HealthChecks.Uris" Version="8.0.1" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net9.0' ">
<PackageVersion Include="Aspire.Hosting.AppHost" Version="9.0.0" />
<PackageVersion Include="Aspire.Hosting.Seq" Version="9.0.0" />
<PackageVersion Include="Aspire.Seq" Version="9.0.0" />
<PackageVersion Include="Aspire.Hosting.PostgreSQL" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery" Version="9.0.0" />
<PackageVersion Include="AspNetCore.HealthChecks.NpgSql" Version="9.0.0" />
<PackageVersion Include="AspNetCore.HealthChecks.Uris" Version="9.0.0" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="MinVer" Version="6.0.0" />
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.10.0" />
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.10.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.10.1" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.10.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.10.0" />
<PackageVersion Include="Serilog" Version="4.2.0.0" />
<PackageVersion Include="Serilog.AspNetCore" Version="8.0.3" />
<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.Sinks.Console" Version="6.0.0" />
<PackageVersion Include="Serilog.Sinks.Grafana.Loki" Version="8.3.0" />
<PackageVersion Include="Serilog.Sinks.OpenTelemetry" Version="4.1.1" />
<PackageVersion Include="Serilog.Sinks.Seq" Version="8.0.0" />
<PackageVersion Include="Aspire.Npgsql" Version="8.1.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="Serilog.Sinks.Spectre" Version="0.5.0" />
</ItemGroup>
</Project>
27 changes: 20 additions & 7 deletions src/SimCube.Aspire/Features/Otlp/OtlpServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ 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, string consoleOutputFormat = ConsoleOutputFormat)
public static void AddOtlpServiceDefaults(
this IHostApplicationBuilder builder,
string consoleOutputFormat = ConsoleOutputFormat,
bool lokiCompatible = false,
bool rawCompactJson = false)
{
if (string.IsNullOrEmpty(consoleOutputFormat))
{
consoleOutputFormat = ConsoleOutputFormat;
}

builder.ConfigureSerilog(consoleOutputFormat);
builder.ConfigureSerilog(consoleOutputFormat, lokiCompatible, rawCompactJson);

builder.ConfigureOpenTelemetry();

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

public static LoggerConfiguration GetLoggerConfiguration(this IConfiguration configuration, string consoleOutputFormat = ConsoleOutputFormat)
public static LoggerConfiguration GetLoggerConfiguration(
this IConfiguration configuration,
string consoleOutputFormat = ConsoleOutputFormat,
bool lokiCompatible = false,
bool rawCompactJson = false)
{
var config = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
Expand All @@ -38,9 +46,14 @@ public static LoggerConfiguration GetLoggerConfiguration(this IConfiguration con
.Enrich.WithSpan()
.Enrich.WithExceptionDetails(new DestructuringOptionsBuilder()
.WithDefaultDestructurers())
.Enrich.WithProperty(nameof(OtlpLiterals.ServiceName), configuration[OtlpLiterals.ServiceName])
.WriteTo.Spectre(outputTemplate: consoleOutputFormat);
.Enrich.WithProperty(nameof(OtlpLiterals.ServiceName), configuration[OtlpLiterals.ServiceName]);

config = lokiCompatible switch
{
true => config.WriteTo.Console(rawCompactJson ? new RenderedCompactJsonFormatter() : new LokiJsonTextFormatter()),
false => config.WriteTo.Spectre(outputTemplate: consoleOutputFormat),
};

var otlpEndpoint = configuration.GetValue(OtlpLiterals.Endpoint, string.Empty);

if (!string.IsNullOrEmpty(otlpEndpoint))
Expand Down Expand Up @@ -81,11 +94,11 @@ public static void MapDefaultEndpoints(this WebApplication app)
});
}

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

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

private static void ConfigureOpenTelemetryLogging(this IHostApplicationBuilder builder) =>
Expand Down
2 changes: 2 additions & 0 deletions src/SimCube.Aspire/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
global using Serilog.Enrichers.Span;
global using Serilog.Exceptions;
global using Serilog.Exceptions.Core;
global using Serilog.Formatting.Compact;
global using Serilog.Sinks.Grafana.Loki;
global using Serilog.Sinks.OpenTelemetry;
global using Serilog.Sinks.Spectre;
global using SimCube.Aspire.Features.Seq;
1 change: 1 addition & 0 deletions src/SimCube.Aspire/SimCube.Aspire.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<PackageReference Include="Serilog.Enrichers.Span"/>
<PackageReference Include="Serilog.Exceptions"/>
<PackageReference Include="Serilog.Sinks.Console"/>
<PackageReference Include="Serilog.Sinks.Grafana.Loki" />
<PackageReference Include="Serilog.Sinks.OpenTelemetry"/>
<PackageReference Include="Serilog.Sinks.Seq" />
<PackageReference Include="Serilog.Sinks.Spectre" />
Expand Down

0 comments on commit d32a3ed

Please sign in to comment.