Skip to content

Instantly share code, notes, and snippets.

@anthonyvscode
Created June 19, 2015 05:22
Show Gist options
  • Save anthonyvscode/2af2ba5c3e94b5c5cf59 to your computer and use it in GitHub Desktop.
Save anthonyvscode/2af2ba5c3e94b5c5cf59 to your computer and use it in GitHub Desktop.
Serilog - Enrich Exception Logs with Server Variables
public class ExceptionEnricher : ILogEventEnricher
{
/// <summary>
/// The server variables added to an exception log.
/// </summary>
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
if (logEvent == null)
throw new ArgumentNullException("logEvent");
if (logEvent.Exception == null) return;
if (HttpContext.Current == null) return;
var context = new HttpContextWrapper(HttpContext.Current);
NameValueCollection collection = context.Request.ServerVariables;
var logEvents = from string key in collection
let value = collection[key]
select new LogEventProperty(key, new ScalarValue(value));
foreach (var log in logEvents)
{
logEvent.AddPropertyIfAbsent(log);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment