Skip to content

Commit

Permalink
Merge branch 'introduce-log-event-ids' of github.com:nmshd/backbone i…
Browse files Browse the repository at this point in the history
…nto introduce-log-event-ids
  • Loading branch information
NikolaVetnic committed Oct 18, 2023
2 parents 849a9d4 + 2f083dd commit 2b775b0
Show file tree
Hide file tree
Showing 16 changed files with 310 additions and 85 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/publish-artifacthub.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Artifact Repo Publish

on:
push:
branches: [main]
paths: [helm/artifacthub-repo.yml]

jobs:
upload-artifact-repo-config:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install oras
run: |
curl -L https://github.com/oras-project/oras/releases/download/v1.1.0/oras_1.1.0_linux_amd64.tar.gz -o oras.tar.gz
tar -xzf oras.tar.gz oras
sudo mv oras /usr/local/bin
rm oras.tar.gz
- name: Docker Login
uses: docker/[email protected]
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: oras push
working-directory: ./helm
run: |
oras push \
ghcr.io/nmshd/backbone-helm-chart:artifacthub.io \
--config /dev/null:application/vnd.cncf.artifacthub.config.v1+yaml \
artifacthub-repo.yml:application/vnd.cncf.artifacthub.repository-metadata.layer.v1.yaml
1 change: 1 addition & 0 deletions AdminUi/src/AdminUi/AdminUi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<PackageReference Include="Serilog.Exceptions.EntityFrameworkCore" Version="8.4.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
<PackageReference Include="Serilog.Sinks.Http" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Seq" Version="5.2.3" />
<!-- CAUTION: Do not upgrade 'AspNetCore.HealthChecks.NpgSql' before the following issue is resolved: https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/issues/1993 -->
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="[6.0.2]" />
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="7.0.0" />
Expand Down
92 changes: 63 additions & 29 deletions AdminUi/src/AdminUi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,79 @@
using Backbone.Modules.Devices.Infrastructure.OpenIddict;
using Backbone.Modules.Devices.Infrastructure.Persistence.Database;
using Enmeshed.BuildingBlocks.API.Extensions;
using Enmeshed.BuildingBlocks.Application.Abstractions.Infrastructure.EventBus;
using Enmeshed.BuildingBlocks.Application.QuotaCheck;
using Enmeshed.BuildingBlocks.Infrastructure.Persistence.Database;
using Enmeshed.Tooling.Extensions;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Logging;
using Serilog;
using Serilog.Exceptions;
using Serilog.Exceptions.Core;
using Serilog.Exceptions.EntityFrameworkCore.Destructurers;
using Serilog.Settings.Configuration;
using LogHelper = Backbone.Infrastructure.Logging.LogHelper;

Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();

var builder = WebApplication.CreateBuilder(args);
builder.WebHost
.UseKestrel(options =>
{
options.AddServerHeader = false;
options.Limits.MaxRequestBodySize = 20.Mebibytes();
});
try
{
Log.Information("Creating app...");

var app = CreateApp(args);

Log.Information("App created.");

Log.Information("Starting app...");

app.Run();

return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}

static WebApplication CreateApp(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.WebHost
.UseKestrel(options =>
{
options.AddServerHeader = false;
options.Limits.MaxRequestBodySize = 20.Mebibytes();
});

LoadConfiguration(builder, args);
LoadConfiguration(builder, args);

builder.Host
.UseSerilog((context, configuration) => configuration
.ReadFrom.Configuration(context.Configuration, new ConfigurationReaderOptions { SectionName = "Logging" })
.Enrich.WithCorrelationId("X-Correlation-Id", addValueIfHeaderAbsence: true)
.Enrich.WithDemystifiedStackTraces()
.Enrich.FromLogContext()
.Enrich.WithExceptionDetails(new DestructuringOptionsBuilder()
.WithDefaultDestructurers()
.WithDestructurers(new[] { new DbUpdateExceptionDestructurer() })))
.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host
.UseSerilog((context, configuration) => configuration
.ReadFrom.Configuration(context.Configuration, new ConfigurationReaderOptions { SectionName = "Logging" })
.Enrich.WithCorrelationId("X-Correlation-Id", addValueIfHeaderAbsence: true)
.Enrich.WithDemystifiedStackTraces()
.Enrich.FromLogContext()
.Enrich.WithProperty("service", "adminui")
.Enrich.WithExceptionDetails(new DestructuringOptionsBuilder()
.WithDefaultDestructurers()
.WithDestructurers(new[] { new DbUpdateExceptionDestructurer() })))
.UseServiceProviderFactory(new AutofacServiceProviderFactory());

ConfigureServices(builder.Services, builder.Configuration, builder.Environment);
ConfigureServices(builder.Services, builder.Configuration, builder.Environment);

var app = builder.Build();
Configure(app);
var app = builder.Build();
Configure(app);

app.MigrateDbContext<AdminUiDbContext>();
app.MigrateDbContext<AdminUiDbContext>();

app.Run();
return app;
}

static void ConfigureServices(IServiceCollection services, IConfiguration configuration, IHostEnvironment environment)
{
Expand Down Expand Up @@ -90,7 +117,8 @@ static void ConfigureServices(IServiceCollection services, IConfiguration config
options
.UseEntityFrameworkCore()
.UseDbContext<DevicesDbContext>()
.ReplaceDefaultEntities<CustomOpenIddictEntityFrameworkCoreApplication, CustomOpenIddictEntityFrameworkCoreAuthorization, CustomOpenIddictEntityFrameworkCoreScope, CustomOpenIddictEntityFrameworkCoreToken, string>();
.ReplaceDefaultEntities<CustomOpenIddictEntityFrameworkCoreApplication, CustomOpenIddictEntityFrameworkCoreAuthorization, CustomOpenIddictEntityFrameworkCoreScope,
CustomOpenIddictEntityFrameworkCoreToken, string>();
options.AddApplicationStore<CustomOpenIddictEntityFrameworkCoreApplicationStore>();
});

Expand Down Expand Up @@ -125,6 +153,12 @@ static void Configure(WebApplication app)

var configuration = app.Services.GetRequiredService<IOptions<AdminConfiguration>>().Value;

app.UseSerilogRequestLogging(opts =>
{
opts.EnrichDiagnosticContext = LogHelper.EnrichFromRequest;
opts.GetLevel = LogHelper.GetLevel;
});

app.UseSecurityHeaders(policies =>
{
policies
Expand All @@ -137,10 +171,10 @@ static void Configure(WebApplication app)
});

if (configuration.SwaggerUi.Enabled)
{
app.UseSwagger().UseSwaggerUI();
IdentityModelEventSource.ShowPII = true;
}

if (app.Environment.IsDevelopment())
Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true;

app.UseCors();

Expand Down
9 changes: 8 additions & 1 deletion AdminUi/src/AdminUi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@
},
"Logging": {
"MinimumLevel": {
"Default": "Error"
"Default": "Warning",
"Override": {
"Backbone": "Information",
"Enmeshed": "Information",
"AdminUi": "Information",

"Microsoft": "Information"
}
},
"WriteTo": {
"Console": {
Expand Down
17 changes: 16 additions & 1 deletion AdminUi/src/AdminUi/appsettings.override.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,22 @@
},
"Logging": {
"MinimumLevel": {
"Default": "Information"
"Default": "Information",
"Override": {
"Backbone": "Debug",
"Enmeshed": "Debug",
"AdminUi": "Debug",

"Microsoft.AspNetCore": "Warning"
}
},
"WriteTo": {
"Seq": {
"Name": "Seq",
"Args": {
"ServerUrl": "http://localhost:5341"
}
}
}
}
}
2 changes: 2 additions & 0 deletions Backbone.Infrastructure/Backbone.Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<PackageReference Include="Autofac" Version="7.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog" Version="3.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
56 changes: 56 additions & 0 deletions Backbone.Infrastructure/Logging/LogHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Microsoft.AspNetCore.Http;
using Serilog;
using Serilog.Events;

namespace Backbone.Infrastructure.Logging;

public static class LogHelper
{
public static LogEventLevel GetLevel(HttpContext ctx, double _, Exception? ex)
{
return ex != null
? LogEventLevel.Error
: ctx.Response.StatusCode > 499
? LogEventLevel.Error
: IsHealthCheckEndpoint(ctx) // Not an error, check if it was a health check
? LogEventLevel.Verbose // Was a health check, use Verbose
: LogEventLevel.Information;
}

private static bool IsHealthCheckEndpoint(HttpContext ctx)
{
var endpoint = ctx.GetEndpoint();
if (endpoint != null)
{
return string.Equals(
endpoint.DisplayName,
"Health checks",
StringComparison.Ordinal);
}

// No endpoint, so not a health check endpoint
return false;
}

public static void EnrichFromRequest(IDiagnosticContext diagnosticContext, HttpContext httpContext)
{
var request = httpContext.Request;

diagnosticContext.Set("Host", request.Host);
diagnosticContext.Set("Protocol", request.Protocol);
diagnosticContext.Set("Scheme", request.Scheme);

if (request.QueryString.HasValue)
{
diagnosticContext.Set("QueryString", request.QueryString.Value);
}

diagnosticContext.Set("ContentType", httpContext.Response.ContentType);

var endpoint = httpContext.GetEndpoint();
if (endpoint != null)
{
diagnosticContext.Set("EndpointName", endpoint.DisplayName);
}
}
}
1 change: 1 addition & 0 deletions ConsumerApi/ConsumerApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<PackageReference Include="Serilog.Exceptions.EntityFrameworkCore" Version="8.4.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
<PackageReference Include="Serilog.Sinks.Http" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Seq" Version="5.2.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 2b775b0

Please sign in to comment.