Code Snippet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| open System
let ts () = DateTime.Now.ToString("o") // ISO-8601
let cw (s:string) = Console.WriteLine(s) // Threadsafe console writer
let cew (s:string) = Console.Error.WriteLine(s) // Threadsafe console error writer
type LogLevel = | Info | Warning | Error
let log l x y =
let msg = sprintf "%s - %A: %A (%A)" (ts()) l x y
match l with
| LogLevel.Error -> cew msg
| LogLevel.Info | LogLevel.Warning -> cw msg
let foo x = try x/0 |> Some with ex -> log LogLevel.Error x ex; None
42 |> foo
|
Code output: