See #34
Native support for destructuring F# types when logging to Serilog.
Install from NuGet:
dotnet add package Destructurama.FSharp
Add Destructure.FSharpTypes()
to your logger configuration:
open Serilog
[<EntryPoint>]
let main argv =
Log.Logger <- LoggerConfiguration()
.Destructure.FSharpTypes()
.WriteTo.Console()
.CreateLogger()
Log.Information("Drawing a {@Shape}", Circle 5.)
0
Discriminated unions like:
type Shape =
| Circle of Radius : double
| Rectangle of Width : double * Height : double
| Arc // ...
let shape = Circle 5.
When logged with Serilog:
Log.Information("Drawing a {@Shape}", shape)
Are printed nicely like:
2015-01-14 16:58:31 [Information] Drawing a Circle { Radius: 5 }
Depending on the log storage you use you will be able to query on the tag as well as the fields (like Radius) from the union.
More samples can be seen by running the project in the Samples
folder using
dotnet run
. Doing so will destructure a union, record, and a tuple with and
without this package to highlight the difference:
➜ samples git:(records) ✗ dotnet run
[13:35:19 INF] Printing a {"quantity": 10, "label": "hi", "Tag": 2, "IsA": false, "IsB": false, "IsC": true, "$type": "C"} with poor destructuring
[13:35:19 INF] Printing a {"quantity": 10, "label": "hi", "$type": "C"} with better destructuring
[13:35:19 INF] Printing a {"FieldA": 10, "OtherField": true, "AnotherOne": {"quantity": 10, "label": "hi", "Tag": 2, "IsA": false, "IsB": false, "IsC": true, "$type": "C"}, "$type": "MyRecord"} with poor destructuring
[13:35:19 INF] Printing a {"FieldA": 10, "OtherField": true, "AnotherOne": {"quantity": 10, "label": "hi", "$type": "C"}, "$type": "MyRecord"} with better destructuring
[13:35:19 INF] Printing a {"Item1": 1, "Item2": "hi", "$type": "Tuple`2"} with poor destructuring
[13:35:19 INF] Printing a [1, "hi"] with better destructuring