mirror of
https://github.com/alex289/CleanArchitecture.git
synced 2025-07-04 04:22:56 +00:00
feat: Add Serilog and Seq logging
This commit is contained in:
parent
ae2f56a7cf
commit
0f5534ffe6
@ -14,6 +14,7 @@
|
|||||||
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="8.0.2" />
|
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="8.0.2" />
|
||||||
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1" />
|
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1" />
|
||||||
<PackageReference Include="Grpc.AspNetCore.Server.Reflection" Version="2.66.0" />
|
<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.Authentication.JwtBearer" Version="8.0.8" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" 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.EntityFrameworkCore.Proxies" Version="8.0.8" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" 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="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" Version="6.8.1" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.8.1" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.8.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -14,10 +14,15 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using OpenTelemetry.Resources;
|
||||||
|
using OpenTelemetry.Trace;
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
builder.Host.UseSerilog((context, loggerConfig) =>
|
||||||
|
loggerConfig.ReadFrom.Configuration(context.Configuration));
|
||||||
|
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
builder.Services.AddGrpc();
|
builder.Services.AddGrpc();
|
||||||
builder.Services.AddGrpcReflection();
|
builder.Services.AddGrpcReflection();
|
||||||
@ -67,12 +72,6 @@ builder.Services.AddHostedService<SetInactiveUsersService>();
|
|||||||
|
|
||||||
builder.Services.AddMediatR(cfg => { cfg.RegisterServicesFromAssemblies(typeof(Program).Assembly); });
|
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"]))
|
if (builder.Environment.IsProduction() || !string.IsNullOrWhiteSpace(builder.Configuration["RedisHostName"]))
|
||||||
{
|
{
|
||||||
builder.Services.AddStackExchangeRedisCache(options =>
|
builder.Services.AddStackExchangeRedisCache(options =>
|
||||||
@ -86,6 +85,19 @@ else
|
|||||||
builder.Services.AddDistributedMemoryCache();
|
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();
|
var app = builder.Build();
|
||||||
|
|
||||||
using (var scope = app.Services.CreateScope())
|
using (var scope = app.Services.CreateScope())
|
||||||
@ -112,6 +124,8 @@ app.UseHttpsRedirection();
|
|||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.UseSerilogRequestLogging();
|
||||||
|
|
||||||
app.MapHealthChecks("/healthz", new HealthCheckOptions
|
app.MapHealthChecks("/healthz", new HealthCheckOptions
|
||||||
{
|
{
|
||||||
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
|
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
|
||||||
|
@ -1,8 +1,26 @@
|
|||||||
{
|
{
|
||||||
"Logging": {
|
"Serilog": {
|
||||||
"LogLevel": {
|
"Using": [
|
||||||
|
"Serilog.Sinks.Console"
|
||||||
|
],
|
||||||
|
"MinimumLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft.AspNetCore": "Warning"
|
"Override": {
|
||||||
|
"Microsoft": "Information"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"WriteTo": [
|
||||||
|
{
|
||||||
|
"Name": "Console"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"Enrich": [
|
||||||
|
"FromLogContext",
|
||||||
|
"WithMachineName",
|
||||||
|
"WithThreadId"
|
||||||
|
],
|
||||||
|
"Properties": {
|
||||||
|
"Application": "CleanArchitecture.Api"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
|
@ -1,8 +1,26 @@
|
|||||||
{
|
{
|
||||||
"Logging": {
|
"Serilog": {
|
||||||
"LogLevel": {
|
"Using": [
|
||||||
|
"Serilog.Sinks.Console"
|
||||||
|
],
|
||||||
|
"MinimumLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft.AspNetCore": "Warning"
|
"Override": {
|
||||||
|
"Microsoft": "Information"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"WriteTo": [
|
||||||
|
{
|
||||||
|
"Name": "Console"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"Enrich": [
|
||||||
|
"FromLogContext",
|
||||||
|
"WithMachineName",
|
||||||
|
"WithThreadId"
|
||||||
|
],
|
||||||
|
"Properties": {
|
||||||
|
"Application": "CleanArchitecture.Api"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"RedisHostName": "",
|
"RedisHostName": "",
|
||||||
|
@ -1,8 +1,31 @@
|
|||||||
{
|
{
|
||||||
"Logging": {
|
"Serilog": {
|
||||||
"LogLevel": {
|
"Using": [
|
||||||
|
"Serilog.Sinks.Console",
|
||||||
|
"Serilog.Sinks.Seq"
|
||||||
|
],
|
||||||
|
"MinimumLevel": {
|
||||||
"Default": "Information",
|
"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": "*",
|
"AllowedHosts": "*",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
version: "3"
|
|
||||||
services:
|
services:
|
||||||
app:
|
app:
|
||||||
|
container_name: ca_app
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: Dockerfile
|
dockerfile: Dockerfile
|
||||||
@ -22,6 +22,8 @@ services:
|
|||||||
- Kestrel__Endpoints__Grpc__Url=http://+:8080
|
- Kestrel__Endpoints__Grpc__Url=http://+:8080
|
||||||
- Kestrel__Endpoints__Grpc__Protocols=Http2
|
- Kestrel__Endpoints__Grpc__Protocols=Http2
|
||||||
- ConnectionStrings__DefaultConnection=Server=db;Database=clean-architecture;Trusted_Connection=False;MultipleActiveResultSets=true;TrustServerCertificate=True;User Id=SA;Password=Password123!#
|
- 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:
|
healthcheck:
|
||||||
test: ["CMD", "curl", "--fail", "http://localhost/healthz"]
|
test: ["CMD", "curl", "--fail", "http://localhost/healthz"]
|
||||||
interval: 30s
|
interval: 30s
|
||||||
@ -29,7 +31,8 @@ services:
|
|||||||
retries: 3
|
retries: 3
|
||||||
|
|
||||||
db:
|
db:
|
||||||
image: mcr.microsoft.com/mssql/server
|
container_name: ca_db
|
||||||
|
image: mcr.microsoft.com/mssql/server:2022-CU14-ubuntu-22.04
|
||||||
environment:
|
environment:
|
||||||
- ACCEPT_EULA=Y
|
- ACCEPT_EULA=Y
|
||||||
- SA_PASSWORD=Password123!#
|
- SA_PASSWORD=Password123!#
|
||||||
@ -37,6 +40,7 @@ services:
|
|||||||
- 1433:1433
|
- 1433:1433
|
||||||
|
|
||||||
redis:
|
redis:
|
||||||
|
container_name: ca_redis
|
||||||
image: docker.io/bitnami/redis:7.2
|
image: docker.io/bitnami/redis:7.2
|
||||||
environment:
|
environment:
|
||||||
# ALLOW_EMPTY_PASSWORD is recommended only for development.
|
# ALLOW_EMPTY_PASSWORD is recommended only for development.
|
||||||
@ -48,6 +52,7 @@ services:
|
|||||||
- 'redis_data:/bitnami/redis/data'
|
- 'redis_data:/bitnami/redis/data'
|
||||||
|
|
||||||
rabbitmq:
|
rabbitmq:
|
||||||
|
container_name: ca_rabbitmq
|
||||||
image: "rabbitmq:3-management"
|
image: "rabbitmq:3-management"
|
||||||
ports:
|
ports:
|
||||||
- 5672:5672
|
- 5672:5672
|
||||||
@ -63,7 +68,19 @@ services:
|
|||||||
timeout: 3s
|
timeout: 3s
|
||||||
retries: 3
|
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:
|
volumes:
|
||||||
|
seq_data:
|
||||||
rabbitmq_data:
|
rabbitmq_data:
|
||||||
redis_data:
|
redis_data:
|
||||||
driver: local
|
driver: local
|
||||||
|
@ -27,7 +27,7 @@ spec:
|
|||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
- name: sql-server
|
- name: sql-server
|
||||||
image: mcr.microsoft.com/mssql/server:latest
|
image: mcr.microsoft.com/mssql/server:2022-CU14-ubuntu-22.04
|
||||||
env:
|
env:
|
||||||
- name: ACCEPT_EULA
|
- name: ACCEPT_EULA
|
||||||
value: "Y"
|
value: "Y"
|
||||||
|
Loading…
Reference in New Issue
Block a user