mirror of https://github.com/Squidex/squidex.git
6 changed files with 112 additions and 4 deletions
@ -0,0 +1,59 @@ |
|||||
|
// ==========================================================================
|
||||
|
// HttpLogAppender.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using Microsoft.AspNetCore.Mvc.Infrastructure; |
||||
|
using Squidex.Infrastructure.Log; |
||||
|
|
||||
|
namespace Squidex.Pipeline |
||||
|
{ |
||||
|
public class ActionContextLogAppender : ILogAppender |
||||
|
{ |
||||
|
private readonly IActionContextAccessor actionContextAccessor; |
||||
|
|
||||
|
public ActionContextLogAppender(IActionContextAccessor actionContextAccessor) |
||||
|
{ |
||||
|
this.actionContextAccessor = actionContextAccessor; |
||||
|
} |
||||
|
|
||||
|
public void Append(IObjectWriter writer) |
||||
|
{ |
||||
|
var actionContext = actionContextAccessor.ActionContext; |
||||
|
|
||||
|
if (actionContext == null) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
var httpContext = actionContext.HttpContext; |
||||
|
|
||||
|
Guid requestId; |
||||
|
|
||||
|
if (httpContext.Items.TryGetValue(nameof(requestId), out var value) && value is Guid requestIdValue) |
||||
|
{ |
||||
|
requestId = requestIdValue; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
httpContext.Items[nameof(requestId)] = requestId = Guid.NewGuid(); |
||||
|
} |
||||
|
|
||||
|
writer.WriteObject("web", w => w |
||||
|
.WriteProperty("requestId", requestId.ToString()) |
||||
|
.WriteProperty("requestPath", httpContext.Request.Path) |
||||
|
.WriteProperty("requestMethod", httpContext.Request.Method) |
||||
|
.WriteObject("routeValues", r => |
||||
|
{ |
||||
|
foreach (var kvp in actionContext.ActionDescriptor.RouteValues) |
||||
|
{ |
||||
|
r.WriteProperty(kvp.Key, kvp.Value); |
||||
|
} |
||||
|
})); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,36 @@ |
|||||
|
// ==========================================================================
|
||||
|
// LogPerformanceAttribute.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Diagnostics; |
||||
|
using Microsoft.AspNetCore.Mvc.Filters; |
||||
|
using Squidex.Infrastructure.Log; |
||||
|
|
||||
|
namespace Squidex.Pipeline |
||||
|
{ |
||||
|
public sealed class LogPerformanceAttribute : ActionFilterAttribute |
||||
|
{ |
||||
|
private readonly ISemanticLog log; |
||||
|
|
||||
|
public LogPerformanceAttribute(ISemanticLog log) |
||||
|
{ |
||||
|
this.log = log; |
||||
|
} |
||||
|
|
||||
|
public override void OnActionExecuting(ActionExecutingContext context) |
||||
|
{ |
||||
|
context.HttpContext.Items["Watch"] = Stopwatch.StartNew(); |
||||
|
} |
||||
|
|
||||
|
public override void OnActionExecuted(ActionExecutedContext context) |
||||
|
{ |
||||
|
var stopWatch = (Stopwatch)context.HttpContext.Items["Watch"]; |
||||
|
|
||||
|
log.LogInformation(w => w.WriteProperty("elapsedRequestMs", stopWatch.ElapsedMilliseconds)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue