Headless CMS and Content Managment Hub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

92 lines
2.9 KiB

// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschränkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Log;
using Squidex.Infrastructure.Security;
namespace Squidex.Web.Pipeline
{
public sealed class RequestLogPerformanceMiddleware : IMiddleware
{
private readonly RequestLogOptions requestLogOptions;
private readonly ISemanticLog log;
public RequestLogPerformanceMiddleware(IOptions<RequestLogOptions> requestLogOptions, ISemanticLog log)
{
Guard.NotNull(requestLogOptions);
Guard.NotNull(log);
this.requestLogOptions = requestLogOptions.Value;
this.log = log;
}
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
var watch = ValueStopwatch.StartNew();
using (Profiler.StartSession())
{
try
{
await next(context);
}
finally
{
var elapsedMs = watch.Stop();
if (requestLogOptions.LogRequests)
{
log.LogInformation((elapsedMs, context), (ctx, w) =>
{
if (requestLogOptions.LogProfiler)
{
Profiler.Session?.Write(w);
}
w.WriteObject("filters", ctx.context, LogFilters);
w.WriteProperty("elapsedRequestMs", ctx.elapsedMs);
});
}
}
}
}
private static void LogFilters(HttpContext httpContext, IObjectWriter c)
{
var app = httpContext.Context().App;
if (app != null)
{
c.WriteProperty("appId", app.Id.ToString());
c.WriteProperty("appName", app.Name);
}
var userId = httpContext.User.OpenIdSubject();
if (!string.IsNullOrWhiteSpace(userId))
{
c.WriteProperty(nameof(userId), userId);
}
var clientId = httpContext.User.OpenIdClientId();
if (!string.IsNullOrWhiteSpace(clientId))
{
c.WriteProperty(nameof(clientId), clientId);
}
var costs = httpContext.Features.Get<IApiCostsFeature>()?.Costs ?? 0;
c.WriteProperty(nameof(costs), costs);
}
}
}