Skip to content

Commit

Permalink
Fix for BadRequest from Loki when sending events with windows new lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Moyer committed Jun 11, 2020
1 parent 2edae53 commit ff4574f
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Serilog.Sinks.Loki/LokiBatchFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public void Format(IEnumerable<LogEvent> logEvents, ITextFormatter formatter, Te
// Some enrichers pass strings with quotes surrounding the values inside the string,
// which results in redundant quotes after serialization and a "bad request" response.
// To avoid this, remove all quotes from the value.
stream.Labels.Add(new LokiLabel(property.Key, property.Value.ToString().Replace("\"", "")));
// We also remove any \r\n newlines and replace with \n new lines to prevent "bad request" responses
stream.Labels.Add(new LokiLabel(property.Key, property.Value.ToString().Replace("\"", "").Replace("\r\n", "\n")));

var localTime = DateTime.Now;
var localTimeAndOffset = new DateTimeOffset(localTime, TimeZoneInfo.Local.GetUtcOffset(localTime));
Expand All @@ -69,7 +70,9 @@ public void Format(IEnumerable<LogEvent> logEvents, ITextFormatter formatter, Te
}
}

stream.Entries.Add(new LokiEntry(time, sb.ToString()));
// Loki doesn't like \r\n for new line, and we can't guarantee the message doesn't have any
// in it, so we replace \r\n with \n on the final message
stream.Entries.Add(new LokiEntry(time, sb.ToString().Replace("\r\n", "\n")));
}

if (content.Streams.Count > 0)
Expand Down

0 comments on commit ff4574f

Please sign in to comment.