Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Back out message pre-rendering #229

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A Serilog sink that writes events to the [Seq](https://datalust.co/seq) structur
[<img alt="Package Logo" src="https://datalust.co/images/seq-nuget.png" width="128px">](https://nuget.org/packages/serilog.sinks.seq)

> [!TIP]
> If you would like to see timing and dependency information in Seq, [SerilogTracing](https://github.com/serilog-tracing/serilog-tracing) is a Serilog extension that supports both logs and traces.
> If you would like to see timing and dependency information in Seq, [SerilogTracing](https://github.com/serilog-tracing/serilog-tracing) is a Serilog extension that can send both logs and traces through this sink.


### Getting started
Expand Down

This file was deleted.

54 changes: 0 additions & 54 deletions src/Serilog.Sinks.Seq/Sinks/Seq/Formatting/Padding.cs

This file was deleted.

53 changes: 23 additions & 30 deletions src/Serilog.Sinks.Seq/Sinks/Seq/SeqCompactJsonFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
using Serilog.Formatting.Json;
using Serilog.Parsing;
using Serilog.Sinks.Seq.Conventions;
using Serilog.Sinks.Seq.Formatting;

// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable PossibleMultipleEnumeration

Expand All @@ -36,60 +34,54 @@ namespace Serilog.Sinks.Seq;
/// implicit SerilogTracing span support.</remarks>
public class SeqCompactJsonFormatter: ITextFormatter
{
static readonly IDottedPropertyNameConvention DottedPropertyNameConvention =
AppContext.TryGetSwitch("Serilog.Parsing.MessageTemplateParser.AcceptDottedPropertyNames", out var accept) && accept ?
new UnflattenDottedPropertyNames() :
new PreserveDottedPropertyNames();

readonly JsonValueFormatter _valueFormatter = new("$type");
readonly IDottedPropertyNameConvention _dottedPropertyNameConvention;
readonly JsonValueFormatter _valueFormatter;
readonly IFormatProvider _formatProvider;


/// <summary>
/// Construct a <see cref="SeqCompactJsonFormatter"/>.
/// </summary>
/// <param name="valueFormatter">A value formatter for <see cref="LogEventPropertyValue"/>s on the event.</param>
/// <param name="formatProvider">An <see cref="IFormatProvider"/> that will be used to render log event tokens.</param>
public SeqCompactJsonFormatter(IFormatProvider? formatProvider = null)
public SeqCompactJsonFormatter(IFormatProvider? formatProvider = null, JsonValueFormatter? valueFormatter = null)
{
var acceptDottedPropertyNames = AppContext.TryGetSwitch("Serilog.Parsing.MessageTemplateParser.AcceptDottedPropertyNames", out var accept) && accept;

_dottedPropertyNameConvention = acceptDottedPropertyNames ?
new UnflattenDottedPropertyNames() :
new PreserveDottedPropertyNames();

_formatProvider = formatProvider ?? CultureInfo.InvariantCulture;
_valueFormatter = valueFormatter ?? new("$type");
}

/// <summary>
/// Format the log event into the output. Subsequent events will be newline-delimited.
/// Format the log event into the output. Successive events will be newline-delimited.
/// </summary>
/// <param name="logEvent">The event to format.</param>
/// <param name="output">The output.</param>
public void Format(LogEvent logEvent, TextWriter output)
{
FormatEvent(logEvent, output, _valueFormatter, _formatProvider);
FormatEvent(logEvent, output);
output.WriteLine();
}

/// <summary>
/// Format the log event into the output.
/// Format the log event into the output, without newline delimiters.
/// </summary>
/// <param name="logEvent">The event to format.</param>
/// <param name="output">The output.</param>
/// <param name="valueFormatter">A value formatter for <see cref="LogEventPropertyValue"/>s on the event.</param>
/// <param name="formatProvider">An <see cref="IFormatProvider"/> that will be used to render log event tokens.</param>
public static void FormatEvent(LogEvent logEvent, TextWriter output, JsonValueFormatter valueFormatter, IFormatProvider formatProvider)
public void FormatEvent(LogEvent logEvent, TextWriter output)
{
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));
if (output == null) throw new ArgumentNullException(nameof(output));
if (valueFormatter == null) throw new ArgumentNullException(nameof(valueFormatter));

output.Write("{\"@t\":\"");
output.Write(logEvent.Timestamp.UtcDateTime.ToString("O"));

output.Write("\",\"@mt\":");
JsonValueFormatter.WriteQuotedJsonString(logEvent.MessageTemplate.Text, output);

if (!formatProvider.Equals(CultureInfo.InvariantCulture))
{
// `@m` is normally created during ingestion, however, it must be sent from the client
// to honour non-default IFormatProviders
output.Write(",\"@m\":");
JsonValueFormatter.WriteQuotedJsonString(
CleanMessageTemplateFormatter.Format(logEvent.MessageTemplate, logEvent.Properties, formatProvider),
output);
}

var tokensWithFormat = logEvent.MessageTemplate.Tokens
.OfType<PropertyToken>()
.Where(pt => pt.Format != null);
Expand All @@ -105,9 +97,10 @@ public static void FormatEvent(LogEvent logEvent, TextWriter output, JsonValueFo
output.Write(delim);
delim = ",";
var space = new StringWriter();
r.Render(logEvent.Properties, space, formatProvider);
r.Render(logEvent.Properties, space, _formatProvider);
JsonValueFormatter.WriteQuotedJsonString(space.ToString(), output);
}

output.Write(']');
}

Expand Down Expand Up @@ -167,7 +160,7 @@ public static void FormatEvent(LogEvent logEvent, TextWriter output, JsonValueFo
}
}

var properties = DottedPropertyNameConvention.ProcessDottedPropertyNames(logEvent.Properties);
var properties = _dottedPropertyNameConvention.ProcessDottedPropertyNames(logEvent.Properties);
foreach (var property in properties)
{
var name = property.Key;
Expand All @@ -184,7 +177,7 @@ public static void FormatEvent(LogEvent logEvent, TextWriter output, JsonValueFo
output.Write(',');
JsonValueFormatter.WriteQuotedJsonString(name, output);
output.Write(':');
valueFormatter.Format(property.Value, output);
_valueFormatter.Format(property.Value, output);
}

output.Write('}');
Expand Down
Loading