destructurama / fsharp

Native support for destructuring F# types when logging to Serilog.
Apache License 2.0
38 stars 10 forks source link

Built-in Result type not destructured #8

Closed cmeeren closed 5 years ago

cmeeren commented 6 years ago

The FSharp.Core built-in Result type (defined here) is not destructured correctly:

Microsoft.FSharp.Core.FSharpResult`2[System.String,System.Object]

It seems it only runs ToString on it. I would expect this to be destructured the same way as other unions.

nblumhardt commented 6 years ago

Thanks, @cmeeren 👍

If anyone has any ideas why this would be the case, a PR would be welcome.

adamchester commented 6 years ago

Thanks @cmeeren, it seems to be due to this union being a ‘Struct‘ and we did not add support for that kind of union yet. I agree we should add support for this in the same way as other unions.

Thanks again 👍

cmeeren commented 6 years ago

Happy to be of help, hope it's a quick fix. ^_^

adamchester commented 6 years ago

It should be a quick fix, we just need to find the APIs that we can use instead of this

            | t when FSharpType.IsUnion t ->
                let case, fields = FSharpValue.GetUnionFields(value, t)
                let properties = (case.GetFields(), fields) ||> Seq.map2 lep
                result <- StructureValue(properties, case.Name)
                true
Zaid-Ajaj commented 6 years ago

FYI, Suave.SerilogExtensions implements a proper destructuring mechanism for F# types (using Json -> LogEvent) see Destructure.fs

adam-c-anderson commented 5 years ago

Is this still really open? Just ran this in a test project:

Log.Logger <-
    LoggerConfiguration()
        .Destructure.FSharpTypes()
        .WriteTo.Console(Serilog.Formatting.Json.JsonFormatter())
        .CreateLogger()

type UnionData =
| Label of labelField:string
| Number of int

Log.Information("UnionData Label case {@label}", Label "Foo")
Log.Information("UnionData Number case {@number}", Number 42)
Log.Information("Result Ok case {@ok}", Ok "Bueno")
Log.Information("Result Error case {@error}", Error "Mal")

...and got this output

{"Timestamp":"2019-05-31T11:50:01.4639470-04:00","Level":"Information","MessageTemplate":"UnionData Label case {@label}","Properties":{"label":{"_typeTag":"Label","labelField":"Foo"}}}
{"Timestamp":"2019-05-31T11:50:01.4767652-04:00","Level":"Information","MessageTemplate":"UnionData Number case {@number}","Properties":{"number":{"_typeTag":"Number","Item":42}}}
{"Timestamp":"2019-05-31T11:50:01.4799127-04:00","Level":"Information","MessageTemplate":"Result Ok case {@ok}","Properties":{"ok":{"_typeTag":"Ok","ResultValue":"Bien"}}}
{"Timestamp":"2019-05-31T11:50:01.4807271-04:00","Level":"Information","MessageTemplate":"Result Error case {@error}","Properties":{"error":{"_typeTag":"Error","ErrorValue":"Mal"}}}
baronfel commented 5 years ago

This same structure of struct-handling will hit struct options (valuesome/valuenone) as well.

baronfel commented 5 years ago

I believe that all that needs to happen here is for #9 to be updated/finished and to upgrade to a more recent version of FSharp.Core (probably at least 4.6.2). Doing this will update the implementations of the FSharpType.Isxxxxx functions to return appropriate results for struct tuples, records, and unions. here's an example from the .net core FSI for 3.0 preview 7:

> let thing = ValueSome "ok";;
[<Struct>]
val thing : string voption = ValueSome "ok"

> let thingty = thing.GetType();;
val thingty : System.Type =
  Microsoft.FSharp.Core.FSharpValueOption`1[System.String]

> FSharp.Reflection.FSharpType.IsUnion thingty;;
val it : bool = true
nblumhardt commented 5 years ago

Thanks for the notes, @baronfel . This library's in need of some maintenance, if you or anyone following along are keen to jump in and modernize things the help would be appreciated. Cheers!

baronfel commented 5 years ago

I was planning on doing just that in a couple days/over the weekend. Are you a maintainer here (I'm not sure if the destructurama org is part of serilog proper)?

nblumhardt commented 5 years ago

I'm a maintainer though I'm not actively maintaining :-) ... I haven't touched F# in nearly 10 years, unfortunately