Add request logging
This commit is contained in:
21
Program.cs
21
Program.cs
@@ -2,6 +2,7 @@ using ConnectionsAPI.Config;
|
||||
using ConnectionsAPI.Database;
|
||||
using ConnectionsAPI.Utility;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Net;
|
||||
|
||||
namespace ConnectionsAPI
|
||||
{
|
||||
@@ -55,10 +56,26 @@ namespace ConnectionsAPI
|
||||
app.UsePathBase("/connections-api");
|
||||
}
|
||||
|
||||
//app.UseHttpsRedirection();
|
||||
|
||||
app.UseFastEndpoints();
|
||||
|
||||
// middleware to log requests and their resulting statuses
|
||||
var loggerFactory = app.Services.GetRequiredService<ILoggerFactory>();
|
||||
var requestLogger = loggerFactory.CreateLogger("ConnectionsAPI.RequestLogger");
|
||||
app.Use(async (ctx, next) =>
|
||||
{
|
||||
await next();
|
||||
if (ctx != null)
|
||||
{
|
||||
// log the path and status of the request/response
|
||||
requestLogger.LogTrace(
|
||||
"{requestPath} -- {requestStatusCode} {requestStatusText}",
|
||||
ctx.Request.Path,
|
||||
ctx.Response.StatusCode,
|
||||
((HttpStatusCode)ctx.Response.StatusCode).ToString()
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
// if not dev env, migrate database
|
||||
if (!app.Environment.IsDevelopment())
|
||||
{
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
"Microsoft.AspNetCore": "Warning",
|
||||
"ConnectionsAPI.RequestLogger": "Trace",
|
||||
"Microsoft.EntityFrameworkCore.Database.Command": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
|
||||
Reference in New Issue
Block a user