0
0
mirror of https://github.com/alex289/CleanArchitecture.git synced 2025-07-01 19:12:57 +00:00

feat: Add Serilog and Seq logging

This commit is contained in:
alex289 2024-10-03 19:32:42 +02:00
parent ae2f56a7cf
commit 0f5534ffe6
No known key found for this signature in database
GPG Key ID: 573F77CD2D87F863
7 changed files with 117 additions and 19 deletions

View File

@ -14,6 +14,7 @@
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="8.0.2" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1" />
<PackageReference Include="Grpc.AspNetCore.Server.Reflection" Version="2.66.0" />
<PackageReference Include="MassTransit.RabbitMQ" Version="8.2.5" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.8" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
@ -23,6 +24,13 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="8.0.8" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.8" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.2" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.Seq" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.8.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.8.1" />
</ItemGroup>

View File

@ -14,10 +14,15 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using Serilog;
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog((context, loggerConfig) =>
loggerConfig.ReadFrom.Configuration(context.Configuration));
builder.Services.AddControllers();
builder.Services.AddGrpc();
builder.Services.AddGrpcReflection();
@ -67,12 +72,6 @@ builder.Services.AddHostedService<SetInactiveUsersService>();
builder.Services.AddMediatR(cfg => { cfg.RegisterServicesFromAssemblies(typeof(Program).Assembly); });
builder.Services.AddLogging(x => x.AddSimpleConsole(console =>
{
console.TimestampFormat = "[yyyy-MM-ddTHH:mm:ss.fff] ";
console.IncludeScopes = true;
}));
if (builder.Environment.IsProduction() || !string.IsNullOrWhiteSpace(builder.Configuration["RedisHostName"]))
{
builder.Services.AddStackExchangeRedisCache(options =>
@ -86,6 +85,19 @@ else
builder.Services.AddDistributedMemoryCache();
}
builder.Services
.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService("CleanArchitecture.Api"))
.WithTracing(tracing =>
{
tracing
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation()
.AddSource(MassTransit.Logging.DiagnosticHeaders.DefaultListenerName);
tracing.AddOtlpExporter();
});
var app = builder.Build();
using (var scope = app.Services.CreateScope())
@ -112,6 +124,8 @@ app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseSerilogRequestLogging();
app.MapHealthChecks("/healthz", new HealthCheckOptions
{
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse

View File

@ -1,8 +1,26 @@
{
"Logging": {
"LogLevel": {
"Serilog": {
"Using": [
"Serilog.Sinks.Console"
],
"MinimumLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
"Override": {
"Microsoft": "Information"
}
},
"WriteTo": [
{
"Name": "Console"
}
],
"Enrich": [
"FromLogContext",
"WithMachineName",
"WithThreadId"
],
"Properties": {
"Application": "CleanArchitecture.Api"
}
},
"ConnectionStrings": {

View File

@ -1,8 +1,26 @@
{
"Logging": {
"LogLevel": {
"Serilog": {
"Using": [
"Serilog.Sinks.Console"
],
"MinimumLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
"Override": {
"Microsoft": "Information"
}
},
"WriteTo": [
{
"Name": "Console"
}
],
"Enrich": [
"FromLogContext",
"WithMachineName",
"WithThreadId"
],
"Properties": {
"Application": "CleanArchitecture.Api"
}
},
"RedisHostName": "",

View File

@ -1,8 +1,31 @@
{
"Logging": {
"LogLevel": {
"Serilog": {
"Using": [
"Serilog.Sinks.Console",
"Serilog.Sinks.Seq"
],
"MinimumLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
"Override": {
"Microsoft": "Information"
}
},
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "Seq",
"Args": { "serverUrl": "http://localhost:5341"}
}
],
"Enrich": [
"FromLogContext",
"WithMachineName",
"WithThreadId"
],
"Properties": {
"Application": "CleanArchitecture.Api"
}
},
"AllowedHosts": "*",

View File

@ -1,6 +1,6 @@
version: "3"
services:
app:
container_name: ca_app
build:
context: .
dockerfile: Dockerfile
@ -22,6 +22,8 @@ services:
- Kestrel__Endpoints__Grpc__Url=http://+:8080
- Kestrel__Endpoints__Grpc__Protocols=Http2
- ConnectionStrings__DefaultConnection=Server=db;Database=clean-architecture;Trusted_Connection=False;MultipleActiveResultSets=true;TrustServerCertificate=True;User Id=SA;Password=Password123!#
- OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:5341/ingest/otlp/v1/traces
- OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
healthcheck:
test: ["CMD", "curl", "--fail", "http://localhost/healthz"]
interval: 30s
@ -29,7 +31,8 @@ services:
retries: 3
db:
image: mcr.microsoft.com/mssql/server
container_name: ca_db
image: mcr.microsoft.com/mssql/server:2022-CU14-ubuntu-22.04
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=Password123!#
@ -37,6 +40,7 @@ services:
- 1433:1433
redis:
container_name: ca_redis
image: docker.io/bitnami/redis:7.2
environment:
# ALLOW_EMPTY_PASSWORD is recommended only for development.
@ -48,6 +52,7 @@ services:
- 'redis_data:/bitnami/redis/data'
rabbitmq:
container_name: ca_rabbitmq
image: "rabbitmq:3-management"
ports:
- 5672:5672
@ -63,7 +68,19 @@ services:
timeout: 3s
retries: 3
seq:
container_name: ca_seq
image: datalust/seq:latest
volumes:
- seq_data:/data
environment:
- ACCEPT_EULA=Y
ports:
- 5341:5341
- 8081:80
volumes:
seq_data:
rabbitmq_data:
redis_data:
driver: local

View File

@ -27,7 +27,7 @@ spec:
spec:
containers:
- name: sql-server
image: mcr.microsoft.com/mssql/server:latest
image: mcr.microsoft.com/mssql/server:2022-CU14-ubuntu-22.04
env:
- name: ACCEPT_EULA
value: "Y"