destructurama / fsharp

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

Fix InvalidCastException on value type lists #4

Closed adamchester closed 9 years ago

adamchester commented 9 years ago

This fixes #3 by using Seq.cast over System.Collections.IEnumerable.

I used the following script to test:

#r "System.Runtime"
#r "System.IO"

type Du = Case1 of IsTrue:bool | Case2 of any:string | Case3
type RecursiveDu = Leaf of s:string | Branch of rdu:RecursiveDu
type NestedDu = First | Second | Third of du:Du * rdu:RecursiveDu
type Record = { f1:string; f2:int; f3:bool; f4:obj option; }
type RecordDu = { du:Du; ndu:NestedDu }

let testCases = [
    box <| Du.Case1(true),                  "Case1 { IsTrue: True }"
    box <| Branch(Leaf("l1")),              "Branch { rdu: Leaf { s: \"l1\" } }"
    box <| { f1="x"; f2=1; f3=false; f4=None},
            """Record { f1: "x", f2: 1, f3: False, f4: null }"""
    box <| { du=Case2(""); ndu=Third(Case3, Branch(Leaf("l")))},
            """RecordDu { du: Case2 { any: "" }, ndu: Third { du: Case3 {  }, rdu: Branch { rdu: Leaf { s: "l" } } } }"""
    box <| [[1;2;3];[1;2;3]],               "[[1, 2, 3], [1, 2, 3]]"
    box <| [[1.1;2.2;3.3];[1.;2.;3.]],      "[[1.1, 2.2, 3.3], [1, 2, 3]]"
    box <| [["abc";"def"];["ghi"]],         """[["abc", "def"], ["ghi"]]"""
    box <| [[Leaf("A");Leaf("B")];[]],      """[[Leaf { s: "A" }, Leaf { s: "B" }], []]"""
    box <| (1,2,3,4,5),                     "[1, 2, 3, 4, 5]"
    box <| ( (1,2,3,4,5), (1,2,3,4,5)),     "[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]"
]

#I "../../packages/Serilog.1.4.204/lib/portable-net45+win+wpa81+wp80+MonoAndroid10+MonoTouch10"
#r "Serilog"

#I "bin/Debug/"
#r "Destructurama.FSharp"

#r "../../packages/Serilog.1.4.204/lib/net45/Serilog.FullNetFx.dll"

open Serilog
let assertLogOutput (value:obj, expected:string) =
    use writer = new System.IO.StringWriter()
    Log.Logger <- LoggerConfiguration()
        .Destructure.FSharpTypes()
        .MinimumLevel.Verbose()
        .WriteTo.TextWriter(writer, outputTemplate="{Message}{NewLine}{Exception}")
        .WriteTo.ColoredConsole(outputTemplate="Success: {Message}{NewLine}{Exception}")
        .CreateLogger()

    Log.Debug("{@value}", value)

    let actual = writer.ToString()
    let expected = expected + System.Environment.NewLine

    if actual <> expected then
        failwithf "actual: %A, expected: %A" actual expected

testCases |> Seq.iter assertLogOutput

Which now gives the following output: serilog destructurama fsharp tuple support

nblumhardt commented 9 years ago

Looks good!