Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create Serilog Enricher to add Request header in logs
public class CorrelationIdEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
if (logEvent == null) throw new ArgumentNullException("logEvent");
if (HttpContext.Current == null)
return;
if (HttpContext.Current.Request == null)
return;
string correlationId = HttpContext.Current.Request.Headers["CorrelationId"];
if (string.IsNullOrWhiteSpace(correlationId))
return;
var correlationIdProperty = new LogEventProperty("CorrelationId", new ScalarValue(correlationId));
logEvent.AddPropertyIfAbsent(correlationIdProperty);
}
}
@vetras

This comment has been minimized.

Copy link

vetras commented Aug 9, 2017

this helped :D

@elena-posea

This comment has been minimized.

Copy link

elena-posea commented Nov 20, 2017

this was also helpful for me, as I had a custom header to look for

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.