https://github.com/serilog/serilog-formatting-compact-reader
A reader for Serilog's compact JSON format
https://github.com/serilog/serilog-formatting-compact-reader
Last synced: 2 months ago
JSON representation
A reader for Serilog's compact JSON format
- Host: GitHub
- URL: https://github.com/serilog/serilog-formatting-compact-reader
- Owner: serilog
- License: apache-2.0
- Created: 2016-10-12T03:50:55.000Z (over 8 years ago)
- Default Branch: dev
- Last Pushed: 2024-12-10T21:35:14.000Z (7 months ago)
- Last Synced: 2025-04-04T03:04:00.147Z (3 months ago)
- Language: C#
- Size: 163 KB
- Stars: 32
- Watchers: 11
- Forks: 16
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Serilog.Formatting.Compact.Reader [](https://ci.appveyor.com/project/serilog/serilog-formatting-compact-reader) [](https://www.nuget.org/packages/serilog.formatting.compact.reader)
This package reads (deserializes) JSON log files created by [Serilog.Formatting.Compact](https://github.com/serilog/serilog-formatting-compact) back into Serilog `LogEvent`s.
### Example
Log events are written to a file using `CompactJsonFormatter`:
```csharp
await using var fileLog = new LoggerConfiguration()
.WriteTo.File(new CompactJsonFormatter(), "log.clef")
.CreateLogger();fileLog.Information("Hello, {@User}", new { Name = "nblumhardt", Id = 101 });
fileLog.Information("Number {N:x8}", 42);
fileLog.Warning("Tags are {Tags}", new[] { "test", "orange" });try
{
throw new DivideByZeroException();
}
catch(Exception ex)
{
fileLog.Error(ex, "Something failed");
}
```This creates a log file with content similar to:
```json
{"@t":"2024-10-12T04:46:58.0554314Z","@mt":"Hello, {@User}","User":{"Name":"nblumhardt","Id":101}}
{"@t":"2024-10-12T04:46:58.0684369Z","@mt":"Number {N:x8}","@r":["0000002a"],"N":42}
{"@t":"2024-10-12T04:46:58.0724384Z","@mt":"Tags are {Tags}","@l":"Warning","Tags":["test","orange"]}
{"@t":"2024-10-12T04:46:58.0904378Z","@mt":"Something failed","@l":"Error", "@x":"System.DivideByZer..."}
```An instance of `LogEventReader` converts each line of the log file back into a `LogEvent`, which can be manipulated, rendered, or written through another Serilog sink:
```csharp
await using var console = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();await using var clef = File.OpenText("log.clef"))
using var reader = new LogEventReader(clef);
while (reader.TryRead(out var evt))
console.Write(evt);
```Output from the logger:

### Limitations
Events deserialized from JSON are for typical purposes just like the original log events. There are two main things to keep in mind:
1. JSON doesn't carry all of the type information necessary to determine if, for example, a number is an `int` or a `float`. JSON.NET does a good job of deserializing anything that it encounters, but you can't rely on the types here being identical.
2. Exceptions deserialized this way aren't instances of the original exception type - all you can do with them is call `ToString()` to get the formatted message and stack trace, which is what 99% of Serilog sinks will do.