2020-04-22 14:23:56 +10:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Serilog.Context;
|
2021-02-12 11:38:50 +01:00
|
|
|
using Umbraco.Cms.Core.Logging.Serilog.Enrichers;
|
2020-06-02 13:28:30 +10:00
|
|
|
using Umbraco.Extensions;
|
2020-04-22 14:23:56 +10:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
namespace Umbraco.Cms.Web.Common.Middleware;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds request based serilog enrichers to the LogContext for each request
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class UmbracoRequestLoggingMiddleware : IMiddleware
|
2020-04-22 14:23:56 +10:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
private readonly HttpRequestIdEnricher _requestIdEnricher;
|
|
|
|
|
private readonly HttpRequestNumberEnricher _requestNumberEnricher;
|
|
|
|
|
private readonly HttpSessionIdEnricher _sessionIdEnricher;
|
|
|
|
|
|
|
|
|
|
public UmbracoRequestLoggingMiddleware(
|
|
|
|
|
HttpSessionIdEnricher sessionIdEnricher,
|
|
|
|
|
HttpRequestNumberEnricher requestNumberEnricher,
|
|
|
|
|
HttpRequestIdEnricher requestIdEnricher)
|
2020-04-22 14:23:56 +10:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
_sessionIdEnricher = sessionIdEnricher;
|
|
|
|
|
_requestNumberEnricher = requestNumberEnricher;
|
|
|
|
|
_requestIdEnricher = requestIdEnricher;
|
|
|
|
|
}
|
2020-04-22 14:23:56 +10:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
|
|
|
|
|
{
|
|
|
|
|
// do not process if client-side request
|
|
|
|
|
if (context.Request.IsClientSideRequest())
|
2020-04-22 14:23:56 +10:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
await next(context);
|
|
|
|
|
return;
|
2020-04-22 14:23:56 +10:00
|
|
|
}
|
|
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// TODO: Need to decide if we want this stuff still, there's new request logging in serilog:
|
|
|
|
|
// https://github.com/serilog/serilog-aspnetcore#request-logging which i think would suffice and replace all of this?
|
|
|
|
|
using (LogContext.Push(_sessionIdEnricher))
|
|
|
|
|
using (LogContext.Push(_requestNumberEnricher))
|
|
|
|
|
using (LogContext.Push(_requestIdEnricher))
|
2020-04-22 14:23:56 +10:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
await next(context);
|
2020-04-22 14:23:56 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|