diff --git a/CleanArchitecture.Api/CleanArchitecture.Api.csproj b/CleanArchitecture.Api/CleanArchitecture.Api.csproj index 8cd588f..513f026 100644 --- a/CleanArchitecture.Api/CleanArchitecture.Api.csproj +++ b/CleanArchitecture.Api/CleanArchitecture.Api.csproj @@ -14,6 +14,7 @@ + @@ -23,6 +24,13 @@ + + + + + + + diff --git a/CleanArchitecture.Api/Program.cs b/CleanArchitecture.Api/Program.cs index 5fb7fad..3fbec2b 100644 --- a/CleanArchitecture.Api/Program.cs +++ b/CleanArchitecture.Api/Program.cs @@ -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(); 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 diff --git a/CleanArchitecture.Api/appsettings.Development.json b/CleanArchitecture.Api/appsettings.Development.json index 44623b8..33f3ed2 100644 --- a/CleanArchitecture.Api/appsettings.Development.json +++ b/CleanArchitecture.Api/appsettings.Development.json @@ -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": { diff --git a/CleanArchitecture.Api/appsettings.Integration.json b/CleanArchitecture.Api/appsettings.Integration.json index 2ae8fa8..16699c4 100644 --- a/CleanArchitecture.Api/appsettings.Integration.json +++ b/CleanArchitecture.Api/appsettings.Integration.json @@ -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": "", diff --git a/CleanArchitecture.Api/appsettings.json b/CleanArchitecture.Api/appsettings.json index eb84fec..6a5cc39 100644 --- a/CleanArchitecture.Api/appsettings.json +++ b/CleanArchitecture.Api/appsettings.json @@ -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": "*", diff --git a/docker-compose.yml b/docker-compose.yml index e0c871b..c05ebab 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/k8s-deployments/sql-server.yml b/k8s-deployments/sql-server.yml index 732f255..4d45188 100644 --- a/k8s-deployments/sql-server.yml +++ b/k8s-deployments/sql-server.yml @@ -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"