Add SEQ logging/middleware
This commit is contained in:
@@ -4,7 +4,7 @@ services:
|
|||||||
context: .
|
context: .
|
||||||
dockerfile: ./src/Imprink.WebApi/Dockerfile
|
dockerfile: ./src/Imprink.WebApi/Dockerfile
|
||||||
expose:
|
expose:
|
||||||
- "80"
|
- "8080"
|
||||||
environment:
|
environment:
|
||||||
- ASPNETCORE_ENVIRONMENT=${ASPNETCORE_ENVIRONMENT}
|
- ASPNETCORE_ENVIRONMENT=${ASPNETCORE_ENVIRONMENT}
|
||||||
- ConnectionStrings__DefaultConnection=Server=${SQL_SERVER};Database=${SQL_DATABASE};User Id=${SQL_USER_ID};Password=${SQL_PASSWORD};Encrypt=false;TrustServerCertificate=true;MultipleActiveResultSets=true;
|
- ConnectionStrings__DefaultConnection=Server=${SQL_SERVER};Database=${SQL_DATABASE};User Id=${SQL_USER_ID};Password=${SQL_PASSWORD};Encrypt=false;TrustServerCertificate=true;MultipleActiveResultSets=true;
|
||||||
@@ -20,6 +20,20 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
- app-network
|
- app-network
|
||||||
|
|
||||||
|
seq:
|
||||||
|
image: datalust/seq
|
||||||
|
expose:
|
||||||
|
- "80"
|
||||||
|
- "5341"
|
||||||
|
ports:
|
||||||
|
- "5341:5341"
|
||||||
|
environment:
|
||||||
|
- ACCEPT_EULA=Y
|
||||||
|
- SEQ_CACHE_SYSTEMRAMTARGET=0.9
|
||||||
|
- BASE_URI=https://impr.ink/seq
|
||||||
|
networks:
|
||||||
|
- app-network
|
||||||
|
|
||||||
webui:
|
webui:
|
||||||
image: node:18-alpine
|
image: node:18-alpine
|
||||||
working_dir: /app
|
working_dir: /app
|
||||||
@@ -45,15 +59,16 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
- app-network
|
- app-network
|
||||||
|
|
||||||
|
|
||||||
mssql:
|
mssql:
|
||||||
image: mcr.microsoft.com/mssql/server:2022-latest
|
image: mcr.microsoft.com/mssql/server:2022-latest
|
||||||
container_name: sqlserver
|
container_name: sqlserver
|
||||||
ports:
|
ports:
|
||||||
- "1433:1433"
|
- "1433:1433"
|
||||||
environment:
|
environment:
|
||||||
SA_PASSWORD: "${SQL_PASSWORD}"
|
- SA_PASSWORD=${SQL_PASSWORD}
|
||||||
ACCEPT_EULA: "Y"
|
- ACCEPT_EULA=Y
|
||||||
|
- MSSQL_AGENT_ENABLED=false
|
||||||
|
- MSSQL_LOG_LEVEL=WARN
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
networks:
|
networks:
|
||||||
- app-network
|
- app-network
|
||||||
@@ -70,6 +85,7 @@ services:
|
|||||||
depends_on:
|
depends_on:
|
||||||
- webapi
|
- webapi
|
||||||
- webui
|
- webui
|
||||||
|
- seq
|
||||||
networks:
|
networks:
|
||||||
- app-network
|
- app-network
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,10 @@ http {
|
|||||||
server webui:3000;
|
server webui:3000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
upstream seq {
|
||||||
|
server seq:80;
|
||||||
|
}
|
||||||
|
|
||||||
server {
|
server {
|
||||||
listen 80;
|
listen 80;
|
||||||
server_name impr.ink;
|
server_name impr.ink;
|
||||||
@@ -47,6 +51,27 @@ http {
|
|||||||
proxy_set_header X-Forwarded-Proto $scheme;
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
location /seq {
|
||||||
|
proxy_pass http://seq;
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
|
||||||
|
proxy_set_header X-Forwarded-Host $host;
|
||||||
|
proxy_set_header X-Forwarded-Server $host;
|
||||||
|
proxy_set_header X-Forwarded-Prefix /seq;
|
||||||
|
|
||||||
|
proxy_set_header Upgrade $http_upgrade;
|
||||||
|
proxy_set_header Connection "upgrade";
|
||||||
|
|
||||||
|
proxy_redirect off;
|
||||||
|
|
||||||
|
rewrite ^/seq$ /seq/ redirect;
|
||||||
|
rewrite ^/seq/(.*)$ /$1 break;
|
||||||
|
}
|
||||||
|
|
||||||
location / {
|
location / {
|
||||||
proxy_pass http://webui/;
|
proxy_pass http://webui/;
|
||||||
proxy_http_version 1.1;
|
proxy_http_version 1.1;
|
||||||
|
|||||||
@@ -15,6 +15,8 @@
|
|||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
|
||||||
|
<PackageReference Include="Serilog.Sinks.Seq" Version="9.0.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.1" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
37
src/Imprink.WebApi/Middleware/RequestTimingMiddleware.cs
Normal file
37
src/Imprink.WebApi/Middleware/RequestTimingMiddleware.cs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
namespace Imprink.WebApi.Middleware;
|
||||||
|
|
||||||
|
public class RequestTimingMiddleware(RequestDelegate next, ILogger<RequestTimingMiddleware> logger)
|
||||||
|
{
|
||||||
|
public async Task InvokeAsync(HttpContext context)
|
||||||
|
{
|
||||||
|
var stopwatch = Stopwatch.StartNew();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await next(context);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
stopwatch.Stop();
|
||||||
|
|
||||||
|
var elapsedMs = stopwatch.ElapsedMilliseconds;
|
||||||
|
var method = context.Request.Method;
|
||||||
|
var path = context.Request.Path;
|
||||||
|
var statusCode = context.Response.StatusCode;
|
||||||
|
|
||||||
|
logger.LogInformation(
|
||||||
|
"Request {Method} {Path} completed in {ElapsedMs}ms with status code {StatusCode}",
|
||||||
|
method, path, elapsedMs, statusCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class RequestTimingMiddlewareExtensions
|
||||||
|
{
|
||||||
|
public static IApplicationBuilder UseRequestTiming(this IApplicationBuilder builder)
|
||||||
|
{
|
||||||
|
return builder.UseMiddleware<RequestTimingMiddleware>();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,17 @@
|
|||||||
using Imprink.WebApi;
|
using Imprink.WebApi;
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
Log.Logger = new LoggerConfiguration()
|
||||||
|
.WriteTo.Seq("http://seq:5341",
|
||||||
|
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information)
|
||||||
|
.Enrich.FromLogContext()
|
||||||
|
.WriteTo.Console()
|
||||||
|
.CreateLogger();
|
||||||
|
|
||||||
|
builder.Host.UseSerilog();
|
||||||
|
|
||||||
Startup.ConfigureServices(builder);
|
Startup.ConfigureServices(builder);
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using Imprink.Domain.Repositories;
|
|||||||
using Imprink.Infrastructure;
|
using Imprink.Infrastructure;
|
||||||
using Imprink.Infrastructure.Database;
|
using Imprink.Infrastructure.Database;
|
||||||
using Imprink.Infrastructure.Repositories;
|
using Imprink.Infrastructure.Repositories;
|
||||||
|
using Imprink.WebApi.Middleware;
|
||||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
@@ -95,6 +96,8 @@ public static class Startup
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.UseRequestTiming();
|
||||||
|
|
||||||
if (env.IsDevelopment())
|
if (env.IsDevelopment())
|
||||||
{
|
{
|
||||||
app.UseSwagger();
|
app.UseSwagger();
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ export async function GET() {
|
|||||||
headers: { Cookie: `access_token=${token}` }
|
headers: { Cookie: `access_token=${token}` }
|
||||||
});
|
});
|
||||||
|
|
||||||
NextResponse.json({ message: 'Access token set in cookie' });
|
return NextResponse.json({ message: 'Access token set in cookie' });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error in /api/token:', error);
|
console.error('Error in /api/token:', error);
|
||||||
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
||||||
|
|||||||
Reference in New Issue
Block a user