mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-05-08 11:32:28 +00:00
Split API into its own project
This commit is contained in:
parent
2aa803f69f
commit
47271d1798
16 changed files with 336 additions and 162 deletions
|
@ -0,0 +1,50 @@
|
|||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Logging;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Startup.Middlewares;
|
||||
|
||||
public class RequestLogMiddleware
|
||||
{
|
||||
private readonly RequestDelegate next;
|
||||
|
||||
public RequestLogMiddleware(RequestDelegate next)
|
||||
{
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
// Logs every request and the response to it
|
||||
// Example: "200, 13ms: GET /LITTLEBIGPLANETPS3_XML/news"
|
||||
// Example: "404, 127ms: GET /asdasd?query=osucookiezi727ppbluezenithtopplayhdhr"
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
Stopwatch requestStopwatch = new();
|
||||
requestStopwatch.Start();
|
||||
|
||||
context.Request.EnableBuffering(); // Allows us to reset the position of Request.Body for later logging
|
||||
|
||||
// Log all headers.
|
||||
// foreach (KeyValuePair<string, StringValues> header in context.Request.Headers) Logger.Log($"{header.Key}: {header.Value}");
|
||||
|
||||
await next(context); // Handle the request so we can get the status code from it
|
||||
|
||||
requestStopwatch.Stop();
|
||||
|
||||
Logger.LogInfo
|
||||
(
|
||||
$"{context.Response.StatusCode}, {requestStopwatch.ElapsedMilliseconds}ms: {context.Request.Method} {context.Request.Path}{context.Request.QueryString}",
|
||||
LogArea.HTTP
|
||||
);
|
||||
|
||||
#if DEBUG
|
||||
// Log post body
|
||||
if (context.Request.Method == "POST")
|
||||
{
|
||||
context.Request.Body.Position = 0;
|
||||
Logger.LogDebug(await new StreamReader(context.Request.Body).ReadToEndAsync(), LogArea.HTTP);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue