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.
 
 
 
 
 

79 lines
2.8 KiB

// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Squidex.Log;
namespace Squidex.Web.Pipeline
{
public sealed class ActionContextLogAppender : ILogAppender
{
private readonly IHttpContextAccessor httpContextAccessor;
private readonly IActionContextAccessor actionContextAccessor;
public ActionContextLogAppender(IHttpContextAccessor httpContextAccessor, IActionContextAccessor actionContextAccessor)
{
this.httpContextAccessor = httpContextAccessor;
this.actionContextAccessor = actionContextAccessor;
}
public void Append(IObjectWriter writer, SemanticLogLevel logLevel, Exception? exception)
{
var httpContext = httpContextAccessor.HttpContext;
if (string.IsNullOrEmpty(httpContext?.Request?.Method))
{
return;
}
var actionContext = actionContextAccessor.ActionContext;
try
{
if (string.IsNullOrEmpty(httpContext?.Request?.Method))
{
return;
}
var requestId = GetRequestId(httpContext);
var logContext = (requestId, context: httpContext, actionContext);
writer.WriteObject("web", logContext, (ctx, w) =>
{
w.WriteProperty("requestId", ctx.requestId);
w.WriteProperty("requestPath", ctx.context.Request.Path);
w.WriteProperty("requestMethod", ctx.context.Request.Method);
var actionContext = ctx.actionContext;
if (actionContext != null)
{
w.WriteObject("routeValues", actionContext.ActionDescriptor.RouteValues, (routeValues, r) =>
{
foreach (var (key, value) in routeValues)
{
r.WriteProperty(key, value);
}
});
}
});
}
catch
{
return;
}
}
private static string GetRequestId(HttpContext httpContext)
{
return Activity.Current?.Id ?? httpContext.TraceIdentifier ?? Guid.NewGuid().ToString();
}
}
}