forked fromserilog/serilog-formatting-compact-reader
- Notifications
You must be signed in to change notification settings - Fork0
A reader for Serilog's compact JSON format
License
NotificationsYou must be signed in to change notification settings
nblumhardt/serilog-formatting-compact-reader
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This package reads (deserializes) JSON log files created bySerilog.Formatting.Compact back into SerilogLogEvents.
Log events are written to a file usingCompactJsonFormatter:
using(varfileLog=newLoggerConfiguration().WriteTo.File(newCompactJsonFormatter(),"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{thrownewDivideByZeroException();}catch(Exceptionex){fileLog.Error(ex,"Something failed");}}
This creates a log file with content similar to:
{"@t":"2016-10-12T04:46:58.0554314Z","@mt":"Hello, {@User}","User":{"Name":"nblumhardt","Id":101}}{"@t":"2016-10-12T04:46:58.0684369Z","@mt":"Number {N:x8}","@r":["0000002a"],"N":42}{"@t":"2016-10-12T04:46:58.0724384Z","@mt":"Tags are {Tags}","@l":"Warning","Tags":["test","orange"]}{"@t":"2016-10-12T04:46:58.0904378Z","@mt":"Something failed","@l":"Error","@x":"System.DivideByZer...<snip>"}An instance ofLogEventReader converts each line of the log file back into aLogEvent, which can be manipulated, rendered, or written through another Serilog sink:
using(varconsole=newLoggerConfiguration().WriteTo.LiterateConsole().CreateLogger()){using(varclef=File.OpenText("log.clef")){varreader=newLogEventReader(clef);LogEventevt;while(reader.TryRead(outevt))console.Write(evt);}}
Output from the logger:
Events deserialized from JSON are for typical purposes just like the original log events. There are two main things to keep in mind:
- JSON doesn't carry all of the type information necessary to determine if, for example, a number is an
intor afloat. JSON.NET does a good job of deserializing anything that it encounters, but you can't rely on the types here being identical. - 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.
About
A reader for Serilog's compact JSON format
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
No releases published
Packages0
No packages published
Languages
- C#94.4%
- PowerShell5.6%
