Skip to content

Commit

Permalink
Centralise logging config over all apps (#940)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad authored Dec 4, 2023
1 parent 2e88595 commit 42f5e7e
Show file tree
Hide file tree
Showing 30 changed files with 256 additions and 201 deletions.
9 changes: 7 additions & 2 deletions TeachingRecordSystem/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<PackageVersion Include="MediatR" Version="12.1.1" />
<PackageVersion Include="Microsoft.ApplicationInsights" Version="2.21.0" />
<PackageVersion Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageVersion Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.0" />
Expand Down Expand Up @@ -64,11 +65,15 @@
<PackageVersion Include="Respawn" Version="6.1.0" />
<PackageVersion Include="Scrutor" Version="4.2.2" />
<PackageVersion Include="Sentry.AspNetCore" Version="3.40.1" />
<PackageVersion Include="Sentry.Extensions.Logging" Version="3.41.3" />
<PackageVersion Include="Sentry.Serilog" Version="3.40.1" />
<PackageVersion Include="Serilog" Version="2.12.0" />
<PackageVersion Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageVersion Include="Serilog.Extensions.Hosting" Version="8.0.0" />
<PackageVersion Include="Serilog.Formatting.Compact" Version="1.1.0" />
<PackageVersion Include="Serilog.Settings.Configuration" Version="7.0.1" />
<PackageVersion Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageVersion Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
<PackageVersion Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageVersion Include="SerilogTimings" Version="3.0.1" />
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
Expand All @@ -79,4 +84,4 @@
<PackageVersion Include="Xunit.DependencyInjection" Version="8.9.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.4" />
</ItemGroup>
</Project>
</Project>
17 changes: 16 additions & 1 deletion TeachingRecordSystem/TeachingRecordSystem.sln
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TeachingRecordSystem.Suppor
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FormFlow", "lib\formflow\src\FormFlow\FormFlow.csproj", "{DA3F55CA-6B95-4A85-A3D6-2997C3149A63}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TeachingRecordSystem.Worker", "src\TeachingRecordSystem.Worker\TeachingRecordSystem.Worker.csproj", "{E7D4BCFD-E610-41E2-B209-0BE13799C7A8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TeachingRecordSystem.Worker", "src\TeachingRecordSystem.Worker\TeachingRecordSystem.Worker.csproj", "{E7D4BCFD-E610-41E2-B209-0BE13799C7A8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TeachingRecordSystem.Hosting", "src\TeachingRecordSystem.Hosting\TeachingRecordSystem.Hosting.csproj", "{BD13EB34-437A-45FE-AE0C-7DAD1E9681C0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -192,6 +194,18 @@ Global
{E7D4BCFD-E610-41E2-B209-0BE13799C7A8}.Release|x64.Build.0 = Release|Any CPU
{E7D4BCFD-E610-41E2-B209-0BE13799C7A8}.Release|x86.ActiveCfg = Release|Any CPU
{E7D4BCFD-E610-41E2-B209-0BE13799C7A8}.Release|x86.Build.0 = Release|Any CPU
{BD13EB34-437A-45FE-AE0C-7DAD1E9681C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BD13EB34-437A-45FE-AE0C-7DAD1E9681C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD13EB34-437A-45FE-AE0C-7DAD1E9681C0}.Debug|x64.ActiveCfg = Debug|Any CPU
{BD13EB34-437A-45FE-AE0C-7DAD1E9681C0}.Debug|x64.Build.0 = Debug|Any CPU
{BD13EB34-437A-45FE-AE0C-7DAD1E9681C0}.Debug|x86.ActiveCfg = Debug|Any CPU
{BD13EB34-437A-45FE-AE0C-7DAD1E9681C0}.Debug|x86.Build.0 = Debug|Any CPU
{BD13EB34-437A-45FE-AE0C-7DAD1E9681C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD13EB34-437A-45FE-AE0C-7DAD1E9681C0}.Release|Any CPU.Build.0 = Release|Any CPU
{BD13EB34-437A-45FE-AE0C-7DAD1E9681C0}.Release|x64.ActiveCfg = Release|Any CPU
{BD13EB34-437A-45FE-AE0C-7DAD1E9681C0}.Release|x64.Build.0 = Release|Any CPU
{BD13EB34-437A-45FE-AE0C-7DAD1E9681C0}.Release|x86.ActiveCfg = Release|Any CPU
{BD13EB34-437A-45FE-AE0C-7DAD1E9681C0}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -209,6 +223,7 @@ Global
{407EB5A5-8B4F-4F7D-80BA-E39AB6AD013C} = {91DCFC76-6636-4AC1-B81C-7F8AE1F22116}
{DA3F55CA-6B95-4A85-A3D6-2997C3149A63} = {837E2941-F1CC-41EF-A652-B605101B2E84}
{E7D4BCFD-E610-41E2-B209-0BE13799C7A8} = {837E2941-F1CC-41EF-A652-B605101B2E84}
{BD13EB34-437A-45FE-AE0C-7DAD1E9681C0} = {837E2941-F1CC-41EF-A652-B605101B2E84}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {61D239F9-888B-4D01-84BC-9276C92383EA}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# syntax=docker/dockerfile:1
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine3.18
ARG GIT_SHA
ENV GitSha ${GIT_SHA}
ENV SENTRY_RELEASE ${GIT_SHA}
ENV ASPNETCORE_HTTP_PORTS 80
COPY src/TeachingRecordSystem.Api/bin/Release/net8.0/publish/ App/
COPY src/TeachingRecordSystem.Cli/bin/Release/net8.0/publish/ TrsCli/
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,75 +1,28 @@
using Microsoft.ApplicationInsights.Extensibility;
using Sentry.AspNetCore;
using Sentry.Extensibility;
using Serilog;
using Serilog.Formatting.Compact;
using TeachingRecordSystem.Api.Infrastructure.ApplicationInsights;
using TeachingRecordSystem.Hosting;

namespace TeachingRecordSystem.Api.Infrastructure.Logging;

public static class WebApplicationBuilderExtensions
{
public static WebApplicationBuilder ConfigureLogging(this WebApplicationBuilder builder, string? platformEnvironmentName)
public static WebApplicationBuilder ConfigureLogging(this WebApplicationBuilder builder)
{
if (builder.Environment.IsProduction())
{
builder.WebHost.UseSentry(options =>
{
options.SetBeforeSend((Sentry.SentryEvent e) =>
{
if (e.Exception is not null && !SentryErrors.ShouldReport(e.Exception))
{
return null;
}

return e;
});
});

builder.Services.AddSingleton<ISentryEventProcessor, RemoveRedactedUrlParametersEventProcessor>();

builder.Services.Configure<SentryAspNetCoreOptions>(options =>
{
if (!string.IsNullOrEmpty(platformEnvironmentName))
{
options.Environment = platformEnvironmentName;
}

var gitSha = builder.Configuration["GitSha"];
if (!string.IsNullOrEmpty(gitSha))
{
options.Release = gitSha;
}
});
builder.WebHost.UseSentry(dsn: builder.Configuration.GetRequiredValue("Sentry:Dsn"));
}

builder.Services.AddSingleton<ISentryEventProcessor, RemoveRedactedUrlParametersEventProcessor>();

builder.Services.AddApplicationInsightsTelemetry()
.AddApplicationInsightsTelemetryProcessor<RedactedUrlTelemetryProcessor>();

// We want all logging to go through Serilog so that our filters are always applied
builder.Logging.ClearProviders();

builder.Host.UseSerilog((ctx, services, config) =>
{
config
.ReadFrom.Configuration(ctx.Configuration)
.Filter.With(new IgnoreSuppressedExceptionsLogEventFilter())
.WriteTo.ApplicationInsights(services.GetRequiredService<TelemetryConfiguration>(), TelemetryConverter.Traces)
.WriteTo.Sentry(o =>
{
o.MinimumBreadcrumbLevel = Serilog.Events.LogEventLevel.Debug;
o.MinimumEventLevel = Serilog.Events.LogEventLevel.Error;
});

if (ctx.HostingEnvironment.IsProduction())
{
config.WriteTo.Console(new CompactJsonFormatter());
}
else
{
config.WriteTo.Console();
}
});
builder.Host.UseSerilog((ctx, services, config) => config.ConfigureSerilog(ctx.HostingEnvironment, ctx.Configuration, services));

return builder;
}
Expand Down
3 changes: 1 addition & 2 deletions TeachingRecordSystem/src/TeachingRecordSystem.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public static void Main(string[] args)
builder.Configuration.AddJsonEnvironmentVariable("AppConfig");
}

var platformEnvironmentName = configuration["PlatformEnvironment"];
builder.ConfigureLogging(platformEnvironmentName);
builder.ConfigureLogging();

string pgConnectionString = new NpgsqlConnectionStringBuilder(configuration.GetRequiredValue("ConnectionStrings:DefaultConnection"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@
<PackageReference Include="NSwag.Examples" />
<PackageReference Include="RedisRateLimiting.AspNetCore" />
<PackageReference Include="Sentry.AspNetCore" />
<PackageReference Include="Sentry.Serilog" />
<PackageReference Include="Serilog.AspNetCore" />
<PackageReference Include="Serilog.Formatting.Compact" />
<PackageReference Include="Serilog.Settings.Configuration" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" />
<PackageReference Include="SerilogTimings" />
</ItemGroup>

<ItemGroup>
Expand All @@ -50,6 +45,7 @@

<ItemGroup>
<ProjectReference Include="..\TeachingRecordSystem.Core\TeachingRecordSystem.Core.csproj" />
<ProjectReference Include="..\TeachingRecordSystem.Hosting\TeachingRecordSystem.Hosting.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"DetailedErrors": true,
"Platform": "Local",
"Serilog": {
"MinimumLevel": {
"Default": "Information",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"Platform": "Local",
"Serilog": {
"MinimumLevel": {
"Default": "Error",
Expand Down
54 changes: 0 additions & 54 deletions TeachingRecordSystem/src/TeachingRecordSystem.Core/SentryErrors.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Formatting.Compact;

namespace TeachingRecordSystem.Hosting;

public static class Extensions
{
public static void ConfigureSerilog(
this LoggerConfiguration config,
IHostEnvironment environment,
IConfiguration configuration,
IServiceProvider services)
{
config
.ReadFrom.Configuration(configuration)
.WriteTo.ApplicationInsights(services.GetRequiredService<TelemetryConfiguration>(), TelemetryConverter.Traces)
.WriteTo.Sentry(o => o.InitializeSdk = false);

if (environment.IsProduction())
{
config.WriteTo.Console(new CompactJsonFormatter());
}
else
{
config.WriteTo.Console();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="Sentry.Serilog" />
<PackageReference Include="Serilog.Formatting.Compact" />
<PackageReference Include="Serilog.Settings.Configuration" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" />
<PackageReference Include="Serilog.Sinks.Console" />
<PackageReference Include="SerilogTimings" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# syntax=docker/dockerfile:1
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine3.18
ARG GIT_SHA
ENV GitSha ${GIT_SHA}
ENV SENTRY_RELEASE ${GIT_SHA}
ENV ASPNETCORE_HTTP_PORTS 80
COPY src/TeachingRecordSystem.SupportUi/bin/Release/net8.0/publish/ App/
WORKDIR /App
Expand Down
Loading

0 comments on commit 42f5e7e

Please sign in to comment.