mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2025-08-17 01:31:42 +02:00
.github
.idea
app
.idea
Desktop
Resources
Server
Data
Database
Download
Endpoints
Service
Middlewares
ServerAuthorizationMiddleware.cs
ServerLoggingMiddleware.cs
Viewer
ServerManager.cs
ServerParameters.cs
ServerStartup.cs
ServerUtils.cs
Server.csproj
State.cs
Utils
.editorconfig
.gitignore
Directory.Build.props
DiscordHistoryTracker.sln
NuGet.Config
Version.cs
build.sh
build.wsl.sh
empty.dht
global.json
tools
web
.gitattributes
.gitignore
LICENSE.md
README.md
41 lines
1.3 KiB
C#
41 lines
1.3 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Threading.Tasks;
|
|
using DHT.Utils.Logging;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Http.Extensions;
|
|
|
|
namespace DHT.Server.Service.Middlewares;
|
|
|
|
sealed class ServerLoggingMiddleware(RequestDelegate next) {
|
|
private static readonly Log Log = Log.ForType<ServerLoggingMiddleware>();
|
|
|
|
public async Task InvokeAsync(HttpContext context) {
|
|
var stopwatch = Stopwatch.StartNew();
|
|
try {
|
|
await next(context);
|
|
} catch (OperationCanceledException) {
|
|
OnFinished(stopwatch, context);
|
|
throw;
|
|
}
|
|
|
|
OnFinished(stopwatch, context);
|
|
}
|
|
|
|
private static void OnFinished(Stopwatch stopwatch, HttpContext context) {
|
|
stopwatch.Stop();
|
|
|
|
HttpRequest request = context.Request;
|
|
long requestLength = request.ContentLength ?? 0L;
|
|
long elapsedMs = stopwatch.ElapsedMilliseconds;
|
|
|
|
if (context.RequestAborted.IsCancellationRequested) {
|
|
Log.Debug("Request to " + request.GetEncodedPathAndQuery() + " (" + requestLength + " B) was cancelled after " + elapsedMs + " ms.");
|
|
}
|
|
else {
|
|
int responseStatus = context.Response.StatusCode;
|
|
Log.Debug("Request to " + request.GetEncodedPathAndQuery() + " (" + requestLength + " B) returned " + responseStatus + ", took " + elapsedMs + " ms.");
|
|
}
|
|
}
|
|
}
|