-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
199 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace AD419.Logging | ||
{ | ||
public class CorrelationIdMiddleware | ||
{ | ||
public const string HeaderKey = "X-Correlation-Id"; | ||
|
||
private readonly RequestDelegate _next; | ||
private readonly ILogger _logger; | ||
|
||
public CorrelationIdMiddleware(RequestDelegate next, ILoggerFactory loggerFactory) | ||
{ | ||
_next = next; | ||
_logger = loggerFactory.CreateLogger<CorrelationIdMiddleware>(); | ||
} | ||
|
||
public async Task Invoke(HttpContext context) | ||
{ | ||
// generate new id | ||
var id = Guid.NewGuid().ToString(); | ||
|
||
// append to response header | ||
context.Response.OnStarting(() => | ||
{ | ||
context.Response.Headers.Add(HeaderKey, id); | ||
return Task.CompletedTask; | ||
}); | ||
|
||
await _next(context); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc.Controllers; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
|
||
namespace AD419.Logging | ||
{ | ||
public class SerilogHttpContextEnricher : ILogEventEnricher | ||
{ | ||
private readonly IHttpContextAccessor _contextAccessor; | ||
private bool _enriching = false; | ||
|
||
public SerilogHttpContextEnricher(IHttpContextAccessor contextAccessor) | ||
{ | ||
_contextAccessor = contextAccessor; | ||
} | ||
|
||
public SerilogHttpContextEnricher() | ||
{ | ||
_contextAccessor = new HttpContextAccessor(); | ||
} | ||
|
||
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) | ||
{ | ||
if (_enriching) | ||
{ | ||
// Prevent infinite recursion that can happen when accessing certain properties, like ISession.Id | ||
return; | ||
} | ||
try | ||
{ | ||
_enriching = true; | ||
var httpContext = _contextAccessor.HttpContext; | ||
if (httpContext == null) | ||
return; | ||
|
||
// Set HttpContext properties | ||
AddOrUpdateProperty(httpContext, logEvent, propertyFactory, "User", () => httpContext.User?.Identity?.Name); | ||
AddOrUpdateProperty(httpContext, logEvent, propertyFactory, "TraceId", () => httpContext.TraceIdentifier); | ||
AddOrUpdateProperty(httpContext, logEvent, propertyFactory, "EndpointName", () => httpContext.GetEndpoint()?.DisplayName); | ||
AddOrUpdateProperty(httpContext, logEvent, propertyFactory, "ResponseContentType", () => httpContext.Response?.ContentType?.ToString()); | ||
AddOrUpdateProperty(httpContext, logEvent, propertyFactory, "CorrelationId", () => httpContext.Response?.Headers?[CorrelationIdMiddleware.HeaderKey]); | ||
} | ||
finally | ||
{ | ||
_enriching = false; | ||
} | ||
} | ||
|
||
private static void AddOrUpdateProperty(HttpContext httpContext, LogEvent logEvent, ILogEventPropertyFactory factory, | ||
string propertyName, Func<object> getValue, string defaultValue = "unknown", bool destructure = true) | ||
{ | ||
// Values retrieved from httpContext can go into and out of scope depending on where in the execution | ||
// pipeline logging occurs. We want to hold onto the most up-to-date value that is not defaultValue. | ||
var itemKey = $"SERILOG_CUSTOM_{propertyName}"; | ||
var value = getValue(); | ||
if (httpContext.Items[itemKey] is ValueTuple<object, LogEventProperty> item) | ||
{ | ||
if (value == null || item.Item1 == value) | ||
{ | ||
logEvent.AddOrUpdateProperty(item.Item2); | ||
return; | ||
} | ||
} | ||
|
||
var newValue = value ?? defaultValue; | ||
var newProperty = factory.CreateProperty(propertyName, newValue, destructure); | ||
httpContext.Items[itemKey] = (newValue, newProperty); | ||
logEvent.AddOrUpdateProperty(newProperty); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters