Created
June 19, 2015 05:22
-
-
Save anthonyvscode/2af2ba5c3e94b5c5cf59 to your computer and use it in GitHub Desktop.
Serilog - Enrich Exception Logs with Server Variables
This file contains hidden or 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
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