Skip to content

Commit 69e2dab

Browse files
authored
Merge pull request #66 from nblumhardt/serilog-4
Update to Serilog 4
2 parents 63537e2 + 35d4901 commit 69e2dab

File tree

8 files changed

+210
-213
lines changed

8 files changed

+210
-213
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ A simple `Hello, {User}` event.
1515
Install from [NuGet](https://nuget.org/packages/Serilog.Formatting.Compact):
1616

1717
```powershell
18-
Install-Package Serilog.Formatting.Compact
18+
dotnet add package Serilog.Formatting.Compact
1919
```
2020

2121
The formatter is used in conjunction with sinks that accept `ITextFormatter`. For example, the [file](https://github.com/serilog/serilog-sinks-file) sink:
@@ -26,6 +26,7 @@ Log.Logger = new LoggerConfiguration()
2626
.CreateLogger();
2727
```
2828
#### XML `<appSettings>` configuration
29+
2930
To specify the formatter in XML `<appSettings>` provide its assembly-qualified type name:
3031

3132
```xml
@@ -154,3 +155,7 @@ Several tools are available for working with the CLEF format.
154155
* **[Compact Log Format Viewer](https://github.com/warrenbuckley/Compact-Log-Format-Viewer)** - a cross-platform viewer for CLEF files
155156
* **[`seqcli`](https://github.com/datalust/seqcli)** - pretty-`print` CLEF files at the command-line, or `ingest` CLEF files into [Seq](https://datalust.co/seq) for search, and analysis
156157
* **[_Serilog.Formatting.Compact.Reader_](https://github.com/serilog/serilog-formatting-compact-reader)** - convert CLEF documents back into Serilog `LogEvent`s
158+
159+
### Customizing output
160+
161+
_Serilog.Formatting.Compact_ is not intended to provide customizable formatters. See [this blog post](https://nblumhardt.com/2021/06/customize-serilog-json-output/) for comprehensive Serilog JSON output customization examples.

appveyor.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ version: '{build}'
22
skip_tags: true
33
image: Visual Studio 2022
44
configuration: Release
5-
install:
6-
- ps: ./Setup.ps1
75
build_script:
8-
- ps: ./Build.ps1
6+
- pwsh: ./Build.ps1
97
test: false
108
artifacts:
119
- path: artifacts/Serilog.*.nupkg

global.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/Serilog.Formatting.Compact/CompactJsonFormatter.cs

Lines changed: 92 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -21,117 +21,117 @@
2121
using Serilog.Parsing;
2222
// ReSharper disable MemberCanBePrivate.Global
2323

24-
namespace Serilog.Formatting.Compact
24+
namespace Serilog.Formatting.Compact;
25+
26+
/// <summary>
27+
/// An <see cref="ITextFormatter"/> that writes events in a compact JSON format.
28+
/// </summary>
29+
public class CompactJsonFormatter: ITextFormatter
2530
{
31+
readonly JsonValueFormatter _valueFormatter;
32+
2633
/// <summary>
27-
/// An <see cref="ITextFormatter"/> that writes events in a compact JSON format.
34+
/// Construct a <see cref="CompactJsonFormatter"/>, optionally supplying a formatter for
35+
/// <see cref="LogEventPropertyValue"/>s on the event.
2836
/// </summary>
29-
public class CompactJsonFormatter: ITextFormatter
37+
/// <param name="valueFormatter">A value formatter, or null.</param>
38+
public CompactJsonFormatter(JsonValueFormatter? valueFormatter = null)
3039
{
31-
readonly JsonValueFormatter _valueFormatter;
32-
33-
/// <summary>
34-
/// Construct a <see cref="CompactJsonFormatter"/>, optionally supplying a formatter for
35-
/// <see cref="LogEventPropertyValue"/>s on the event.
36-
/// </summary>
37-
/// <param name="valueFormatter">A value formatter, or null.</param>
38-
public CompactJsonFormatter(JsonValueFormatter valueFormatter = null)
39-
{
40-
_valueFormatter = valueFormatter ?? new JsonValueFormatter(typeTagName: "$type");
41-
}
40+
_valueFormatter = valueFormatter ?? new JsonValueFormatter(typeTagName: "$type");
41+
}
4242

43-
/// <summary>
44-
/// Format the log event into the output. Subsequent events will be newline-delimited.
45-
/// </summary>
46-
/// <param name="logEvent">The event to format.</param>
47-
/// <param name="output">The output.</param>
48-
public void Format(LogEvent logEvent, TextWriter output)
49-
{
50-
FormatEvent(logEvent, output, _valueFormatter);
51-
output.WriteLine();
52-
}
43+
/// <summary>
44+
/// Format the log event into the output. Subsequent events will be newline-delimited.
45+
/// </summary>
46+
/// <param name="logEvent">The event to format.</param>
47+
/// <param name="output">The output.</param>
48+
public void Format(LogEvent logEvent, TextWriter output)
49+
{
50+
FormatEvent(logEvent, output, _valueFormatter);
51+
output.WriteLine();
52+
}
5353

54-
/// <summary>
55-
/// Format the log event into the output.
56-
/// </summary>
57-
/// <param name="logEvent">The event to format.</param>
58-
/// <param name="output">The output.</param>
59-
/// <param name="valueFormatter">A value formatter for <see cref="LogEventPropertyValue"/>s on the event.</param>
60-
public static void FormatEvent(LogEvent logEvent, TextWriter output, JsonValueFormatter valueFormatter)
61-
{
62-
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));
63-
if (output == null) throw new ArgumentNullException(nameof(output));
64-
if (valueFormatter == null) throw new ArgumentNullException(nameof(valueFormatter));
54+
/// <summary>
55+
/// Format the log event into the output.
56+
/// </summary>
57+
/// <param name="logEvent">The event to format.</param>
58+
/// <param name="output">The output.</param>
59+
/// <param name="valueFormatter">A value formatter for <see cref="LogEventPropertyValue"/>s on the event.</param>
60+
public static void FormatEvent(LogEvent logEvent, TextWriter output, JsonValueFormatter valueFormatter)
61+
{
62+
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));
63+
if (output == null) throw new ArgumentNullException(nameof(output));
64+
if (valueFormatter == null) throw new ArgumentNullException(nameof(valueFormatter));
6565

66-
output.Write("{\"@t\":\"");
67-
output.Write(logEvent.Timestamp.UtcDateTime.ToString("O"));
68-
output.Write("\",\"@mt\":");
69-
JsonValueFormatter.WriteQuotedJsonString(logEvent.MessageTemplate.Text, output);
66+
output.Write("{\"@t\":\"");
67+
output.Write(logEvent.Timestamp.UtcDateTime.ToString("O"));
68+
output.Write("\",\"@mt\":");
69+
JsonValueFormatter.WriteQuotedJsonString(logEvent.MessageTemplate.Text, output);
7070

71-
var tokensWithFormat = logEvent.MessageTemplate.Tokens
72-
.OfType<PropertyToken>()
73-
.Where(pt => pt.Format != null);
71+
var tokensWithFormat = logEvent.MessageTemplate.Tokens
72+
.OfType<PropertyToken>()
73+
.Where(pt => pt.Format != null);
7474

75-
// Better not to allocate an array in the 99.9% of cases where this is false
75+
// Better not to allocate an array in the 99.9% of cases where this is false
76+
// ReSharper disable once PossibleMultipleEnumeration
77+
if (tokensWithFormat.Any())
78+
{
79+
output.Write(",\"@r\":[");
80+
var delim = "";
7681
// ReSharper disable once PossibleMultipleEnumeration
77-
if (tokensWithFormat.Any())
82+
foreach (var r in tokensWithFormat)
7883
{
79-
output.Write(",\"@r\":[");
80-
var delim = "";
81-
foreach (PropertyToken r in tokensWithFormat)
82-
{
83-
output.Write(delim);
84-
delim = ",";
85-
var space = new StringWriter();
86-
r.Render(logEvent.Properties, space, CultureInfo.InvariantCulture);
87-
JsonValueFormatter.WriteQuotedJsonString(space.ToString(), output);
88-
}
89-
output.Write(']');
84+
output.Write(delim);
85+
delim = ",";
86+
var space = new StringWriter();
87+
r.Render(logEvent.Properties, space, CultureInfo.InvariantCulture);
88+
JsonValueFormatter.WriteQuotedJsonString(space.ToString(), output);
9089
}
90+
output.Write(']');
91+
}
9192

92-
if (logEvent.Level != LogEventLevel.Information)
93-
{
94-
output.Write(",\"@l\":\"");
95-
output.Write(logEvent.Level);
96-
output.Write('\"');
97-
}
93+
if (logEvent.Level != LogEventLevel.Information)
94+
{
95+
output.Write(",\"@l\":\"");
96+
output.Write(logEvent.Level);
97+
output.Write('\"');
98+
}
9899

99-
if (logEvent.Exception != null)
100-
{
101-
output.Write(",\"@x\":");
102-
JsonValueFormatter.WriteQuotedJsonString(logEvent.Exception.ToString(), output);
103-
}
100+
if (logEvent.Exception != null)
101+
{
102+
output.Write(",\"@x\":");
103+
JsonValueFormatter.WriteQuotedJsonString(logEvent.Exception.ToString(), output);
104+
}
104105

105-
if (logEvent.TraceId != null)
106-
{
107-
output.Write(",\"@tr\":\"");
108-
output.Write(logEvent.TraceId.Value.ToHexString());
109-
output.Write('\"');
110-
}
106+
if (logEvent.TraceId != null)
107+
{
108+
output.Write(",\"@tr\":\"");
109+
output.Write(logEvent.TraceId.Value.ToHexString());
110+
output.Write('\"');
111+
}
111112

112-
if (logEvent.SpanId != null)
113-
{
114-
output.Write(",\"@sp\":\"");
115-
output.Write(logEvent.SpanId.Value.ToHexString());
116-
output.Write('\"');
117-
}
113+
if (logEvent.SpanId != null)
114+
{
115+
output.Write(",\"@sp\":\"");
116+
output.Write(logEvent.SpanId.Value.ToHexString());
117+
output.Write('\"');
118+
}
118119

119-
foreach (var property in logEvent.Properties)
120+
foreach (var property in logEvent.Properties)
121+
{
122+
var name = property.Key;
123+
if (name.Length > 0 && name[0] == '@')
120124
{
121-
var name = property.Key;
122-
if (name.Length > 0 && name[0] == '@')
123-
{
124-
// Escape first '@' by doubling
125-
name = '@' + name;
126-
}
127-
128-
output.Write(',');
129-
JsonValueFormatter.WriteQuotedJsonString(name, output);
130-
output.Write(':');
131-
valueFormatter.Format(property.Value, output);
125+
// Escape first '@' by doubling
126+
name = '@' + name;
132127
}
133128

134-
output.Write('}');
129+
output.Write(',');
130+
JsonValueFormatter.WriteQuotedJsonString(name, output);
131+
output.Write(':');
132+
valueFormatter.Format(property.Value, output);
135133
}
134+
135+
output.Write('}');
136136
}
137-
}
137+
}

src/Serilog.Formatting.Compact/EventIdHash.cs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,38 @@
1414

1515
using System;
1616

17-
namespace Serilog.Formatting.Compact
17+
namespace Serilog.Formatting.Compact;
18+
19+
/// <summary>
20+
/// Hash functions for message templates. See <see cref="Compute"/>.
21+
/// </summary>
22+
public static class EventIdHash
1823
{
1924
/// <summary>
20-
/// Hash functions for message templates. See <see cref="Compute"/>.
25+
/// Compute a 32-bit hash of the provided <paramref name="messageTemplate"/>.
26+
/// The resulting hash value can be uses as an event id in lieu of transmitting
27+
/// the full template string.
2128
/// </summary>
22-
public static class EventIdHash
29+
/// <param name="messageTemplate">A message template.</param>
30+
/// <returns>A 32-bit hash of the template.</returns>
31+
public static uint Compute(string messageTemplate)
2332
{
24-
/// <summary>
25-
/// Compute a 32-bit hash of the provided <paramref name="messageTemplate"/>.
26-
/// The resulting hash value can be uses as an event id in lieu of transmitting
27-
/// the full template string.
28-
/// </summary>
29-
/// <param name="messageTemplate">A message template.</param>
30-
/// <returns>A 32-bit hash of the template.</returns>
31-
public static uint Compute(string messageTemplate)
32-
{
33-
if (messageTemplate == null) throw new ArgumentNullException(nameof(messageTemplate));
33+
if (messageTemplate == null) throw new ArgumentNullException(nameof(messageTemplate));
3434

35-
// Jenkins one-at-a-time https://en.wikipedia.org/wiki/Jenkins_hash_function
36-
unchecked
35+
// Jenkins one-at-a-time https://en.wikipedia.org/wiki/Jenkins_hash_function
36+
unchecked
37+
{
38+
uint hash = 0;
39+
for (var i = 0; i < messageTemplate.Length; ++i)
3740
{
38-
uint hash = 0;
39-
for (var i = 0; i < messageTemplate.Length; ++i)
40-
{
41-
hash += messageTemplate[i];
42-
hash += (hash << 10);
43-
hash ^= (hash >> 6);
44-
}
45-
hash += (hash << 3);
46-
hash ^= (hash >> 11);
47-
hash += (hash << 15);
48-
return hash;
41+
hash += messageTemplate[i];
42+
hash += (hash << 10);
43+
hash ^= (hash >> 6);
4944
}
45+
hash += (hash << 3);
46+
hash ^= (hash >> 11);
47+
hash += (hash << 15);
48+
return hash;
5049
}
5150
}
52-
}
51+
}

0 commit comments

Comments
 (0)