/// <summary> /// Parses a Serilog <see cref="LogEvent"/> and extracts the /// underlying <see cref="LogEntry"/> which is then transformed /// in a serializable DTO. /// </summary> protected virtual LogEntryDto ExtractLogEntry(LogEvent logEvent) { try { logEvent.Properties.TryGetValue(Logger.EntryPropertyName, out var entryProperty); var logEntry = (entryProperty as ScalarValue)?.Value as LogEntry; if (logEntry == null) { //something went wrong logEntry = new LogEntry { TimestampOverride = logEvent.Timestamp, LogLevel = LogLevel.Fatal, Exception = logEvent.Exception, Context = $"{nameof(HttpSink)}.Error", Payload = new { Error = "Could not unwrap log entry.", Message = logEvent.RenderMessage(), Level = logEvent.Level.ToString() } }; } var dto = LogEntryParser.ParseLogEntry(logEntry, AppName, Environment); return(dto); } catch (Exception e) { return(ProcessLoggingException(e)); } }
/// <summary> /// Parses a Serilog <see cref="LogEvent"/> and extracts the /// underlying <see cref="LogEntry"/> which is then transformed /// in a serializable DTO. /// </summary> protected virtual LogEntryDto ExtractLogEntry(LogEvent logEvent) { try { logEvent.Properties.TryGetValue(Logger.EntryPropertyName, out var entryProperty); var logEntry = (entryProperty as ScalarValue)?.Value as LogEntry; if (logEntry == null) { //no log entry to unwrap - assume just a regular Serilog message that didn't come through the custom API var logLevel = ParseLevel(logEvent.Level); logEntry = new LogEntry { TimestampOverride = logEvent.Timestamp, LogLevel = logLevel, Message = logEvent.RenderMessage(), Exception = logEvent.Exception, Context = $"{AppName}.{logLevel}" }; } //extract all other properties and add them to the context object var props = logEvent.Properties.Where(p => !p.Key.Equals(Logger.EntryPropertyName)); foreach (var prop in props) { var scalarValue = prop.Value as ScalarValue; if (scalarValue != null) { //insert (override duplicate keys) logEntry.ContextData[prop.Key] = scalarValue.Value; } } var dto = LogEntryParser.ParseLogEntry(logEntry, AppName, Environment); return(dto); } catch (Exception e) { return(ProcessLoggingException(e)); } }