@@ -417,6 +417,8 @@ module Providers =
417
417
let isAvailable () = true
418
418
419
419
type private ConsoleProvider () =
420
+ let propertyStack = System.Collections.Generic.Stack< string * obj>()
421
+
420
422
let threadSafeWriter = MailboxProcessor.Start( fun inbox ->
421
423
let rec loop () = async {
422
424
let! ( consoleColor , message : string ) = inbox.Receive()
@@ -439,6 +441,7 @@ module Providers =
439
441
( LogLevel.Debug, ConsoleColor.Gray)
440
442
( LogLevel.Trace, ConsoleColor.DarkGray)
441
443
])
444
+
442
445
let writeMessage name logLevel ( messageFunc : MessageThunk ) ``exception`` formatParams =
443
446
match messageFunc with
444
447
| None -> true
@@ -448,26 +451,44 @@ module Providers =
448
451
| Some color -> color
449
452
| None -> Console.ForegroundColor
450
453
let formattedMsg =
451
- let msg = String.Format( CultureInfo.InvariantCulture, m (), formatParams)
454
+ let mutable msg = m ()
455
+
456
+ // have to do name replacements first
457
+ for ( propertyName, propertyValue) in ( Seq.rev propertyStack) do
458
+ let name = sprintf " {%s }" propertyName
459
+ let value = sprintf " %A " propertyValue
460
+ msg <- msg.Replace( name, value)
461
+
462
+ // then c# numeric replacements
463
+ let msg = String.Format( CultureInfo.InvariantCulture, msg , formatParams)
464
+
465
+ // then exception
452
466
let msg =
453
467
match `` exception `` with
454
468
| Some ( e : exn) ->
455
469
String.Format( " {0} | {1}" , msg, e.ToString())
456
470
| None ->
457
471
msg
472
+
473
+ // stitch it all together
458
474
String.Format( " {0} | {1} | {2} | {3}" , DateTime.UtcNow, logLevel, name, msg)
459
475
460
476
threadSafeWriter.Post( color, formattedMsg)
461
477
true
462
478
479
+ let addProp key value =
480
+ propertyStack.Push( key, value)
481
+ { new IDisposable with
482
+ member __.Dispose () = propertyStack.Pop () |> ignore }
483
+
463
484
interface ILogProvider with
464
485
465
486
member this.GetLogger ( name : string ): Logger =
466
487
writeMessage name
467
- member this.OpenMappedContext ( arg1 : string ) ( arg2 : obj ) ( arg3 : bool ): System.IDisposable =
468
- failwith " Not Implemented "
469
- member this.OpenNestedContext ( arg1 : string ): System.IDisposable =
470
- failwith " Not Implemented "
488
+ member this.OpenMappedContext ( key : string ) ( value : obj ) ( destructure : bool ): System.IDisposable =
489
+ addProp key value
490
+ member this.OpenNestedContext ( message : string ): System.IDisposable =
491
+ addProp " NDC " message
471
492
472
493
let create () =
473
494
ConsoleProvider () :> ILogProvider
0 commit comments