-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(formatting): add 4 different JSON formatting types
Add support for the formatting types: - FormattingType.NormalRendered - FormattingType.Normal - FormattingType.CompactRendered - FormattingType.Compact The formatting type can be configured via Options and DurableOptions.
- Loading branch information
1 parent
44d27b2
commit abfb1c6
Showing
31 changed files
with
957 additions
and
121 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2015-2016 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
namespace Serilog.Sinks.Http | ||
{ | ||
/// <summary> | ||
/// Enum defining how log events are formatted when sent over the network. | ||
/// </summary> | ||
public enum FormattingType | ||
{ | ||
/// <summary> | ||
/// The log event is normally formatted and the message template is rendered into a message. | ||
/// This is the most verbose formatting type and its network load is higher than the other | ||
/// options. | ||
/// </summary> | ||
NormalRendered, | ||
|
||
/// <summary> | ||
/// The log event is normally formatted and its data normalized. The lack of a rendered message | ||
/// means improved network load compared to <see cref="NormalRendered"/>. Often this formatting | ||
/// type is complemented with a log server that is capable of rendering the messages of the | ||
/// incoming log events. | ||
/// </summary> | ||
Normal, | ||
|
||
/// <summary> | ||
/// The log event is formatted with minimizing size as a priority but still render the message | ||
/// template into a message. This formatting type greatly reduce the network load and should be | ||
/// used in situations where bandwidth is of importance. | ||
/// </summary> | ||
CompactRendered, | ||
|
||
/// <summary> | ||
/// The log event is formatted with minimizing size as a priority and its data is normalized. The | ||
/// lack of a rendered message means even smaller network load compared to | ||
/// <see cref="CompactRendered"/> and should be used in situations where bandwidth is of | ||
/// importance. Often this formatting type is complemented with a log server that is capable of | ||
/// rendering the messages of the incoming log events. | ||
/// </summary> | ||
Compact | ||
} | ||
} |
This file contains 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
134 changes: 134 additions & 0 deletions
134
src/Serilog.Sinks.Http/Sinks/Http/Private/Formatters/CompactJsonFormatter.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Copyright 2015-2016 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using Serilog.Debugging; | ||
using Serilog.Events; | ||
using Serilog.Formatting; | ||
using Serilog.Formatting.Json; | ||
using Serilog.Parsing; | ||
|
||
namespace Serilog.Sinks.Http.Private.Formatters | ||
{ | ||
internal class CompactJsonFormatter : ITextFormatter | ||
{ | ||
private static readonly JsonValueFormatter ValueFormatter = new JsonValueFormatter(); | ||
|
||
private readonly bool isRenderingMessage; | ||
|
||
public CompactJsonFormatter(bool isRenderingMessage) | ||
{ | ||
this.isRenderingMessage = isRenderingMessage; | ||
} | ||
|
||
public void Format(LogEvent logEvent, TextWriter output) | ||
{ | ||
try | ||
{ | ||
var buffer = new StringWriter(); | ||
FormatContent(logEvent, buffer); | ||
|
||
// If formatting was successful, write to output | ||
output.WriteLine(buffer.ToString()); | ||
} | ||
catch (Exception e) | ||
{ | ||
LogNonFormattableEvent(logEvent, e); | ||
} | ||
} | ||
|
||
private void FormatContent(LogEvent logEvent, TextWriter output) | ||
{ | ||
if (logEvent == null) | ||
throw new ArgumentNullException(nameof(logEvent)); | ||
if (output == null) | ||
throw new ArgumentNullException(nameof(output)); | ||
|
||
output.Write("{\"@t\":\""); | ||
output.Write(logEvent.Timestamp.UtcDateTime.ToString("o")); | ||
|
||
output.Write("\",\"@mt\":"); | ||
JsonValueFormatter.WriteQuotedJsonString(logEvent.MessageTemplate.Text, output); | ||
|
||
if (isRenderingMessage) | ||
{ | ||
output.Write(",\"@m\":"); | ||
var message = logEvent.MessageTemplate.Render(logEvent.Properties); | ||
JsonValueFormatter.WriteQuotedJsonString(message, output); | ||
} | ||
|
||
var tokensWithFormat = logEvent.MessageTemplate.Tokens | ||
.OfType<PropertyToken>() | ||
.Where(pt => pt.Format != null); | ||
|
||
// Better not to allocate an array in the 99.9% of cases where this is false | ||
// ReSharper disable once PossibleMultipleEnumeration | ||
if (tokensWithFormat.Any()) | ||
{ | ||
output.Write(",\"@r\":["); | ||
var delim = ""; | ||
foreach (var r in tokensWithFormat) | ||
{ | ||
output.Write(delim); | ||
delim = ","; | ||
var space = new StringWriter(); | ||
r.Render(logEvent.Properties, space); | ||
JsonValueFormatter.WriteQuotedJsonString(space.ToString(), output); | ||
} | ||
output.Write(']'); | ||
} | ||
|
||
if (logEvent.Level != LogEventLevel.Information) | ||
{ | ||
output.Write(",\"@l\":\""); | ||
output.Write(logEvent.Level); | ||
output.Write('\"'); | ||
} | ||
|
||
if (logEvent.Exception != null) | ||
{ | ||
output.Write(",\"@x\":"); | ||
JsonValueFormatter.WriteQuotedJsonString(logEvent.Exception.ToString(), output); | ||
} | ||
|
||
foreach (var property in logEvent.Properties) | ||
{ | ||
var name = property.Key; | ||
if (name.Length > 0 && name[0] == '@') | ||
{ | ||
// Escape first '@' by doubling | ||
name = '@' + name; | ||
} | ||
|
||
output.Write(','); | ||
JsonValueFormatter.WriteQuotedJsonString(name, output); | ||
output.Write(':'); | ||
ValueFormatter.Format(property.Value, output); | ||
} | ||
|
||
output.Write('}'); | ||
} | ||
|
||
private static void LogNonFormattableEvent(LogEvent logEvent, Exception e) | ||
{ | ||
SelfLog.WriteLine( | ||
"Event at {0} with message template {1} could not be formatted into JSON and will be dropped: {2}", | ||
logEvent.Timestamp.ToString("o"), | ||
logEvent.MessageTemplate.Text, | ||
e); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/Serilog.Sinks.Http/Sinks/Http/Private/Formatters/Converter.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2015-2016 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using Serilog.Formatting; | ||
|
||
namespace Serilog.Sinks.Http.Private.Formatters | ||
{ | ||
internal static class Converter | ||
{ | ||
public static ITextFormatter ToFormatter(FormattingType formattingType) | ||
{ | ||
switch (formattingType) | ||
{ | ||
case FormattingType.NormalRendered: | ||
return new NormalJsonFormatter(true); | ||
|
||
case FormattingType.Normal: | ||
return new NormalJsonFormatter(false); | ||
|
||
case FormattingType.CompactRendered: | ||
return new CompactJsonFormatter(true); | ||
|
||
case FormattingType.Compact: | ||
return new CompactJsonFormatter(false); | ||
|
||
default: | ||
throw new ArgumentException($"Formatting type {formattingType} is not supported"); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.