Skip to content

implement simple propertyname logging for the console logger #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/FsLibLog/FsLibLog.fs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ module Providers =
let isAvailable () = true

type private ConsoleProvider () =
let propertyStack = System.Collections.Generic.Stack<string * obj>()

let threadSafeWriter = MailboxProcessor.Start(fun inbox ->
let rec loop () = async {
let! (consoleColor, message : string) = inbox.Receive()
Expand All @@ -439,6 +441,7 @@ module Providers =
(LogLevel.Debug, ConsoleColor.Gray)
(LogLevel.Trace, ConsoleColor.DarkGray)
])

let writeMessage name logLevel (messageFunc : MessageThunk) ``exception`` formatParams =
match messageFunc with
| None -> true
Expand All @@ -448,26 +451,44 @@ module Providers =
| Some color -> color
| None -> Console.ForegroundColor
let formattedMsg =
let msg = String.Format(CultureInfo.InvariantCulture, m (), formatParams)
let mutable msg = m ()

// have to do name replacements first
for (propertyName, propertyValue) in (Seq.rev propertyStack) do
let name = sprintf "{%s}" propertyName
let value = sprintf "%A" propertyValue
msg <- msg.Replace(name, value)

// then c# numeric replacements
let msg = String.Format(CultureInfo.InvariantCulture, msg , formatParams)

// then exception
let msg =
match ``exception`` with
| Some (e : exn) ->
String.Format("{0} | {1}", msg, e.ToString())
| None ->
msg

// stitch it all together
String.Format("{0} | {1} | {2} | {3}", DateTime.UtcNow, logLevel, name, msg)

threadSafeWriter.Post(color, formattedMsg)
true

let addProp key value =
propertyStack.Push(key, value)
{ new IDisposable with
member __.Dispose () = propertyStack.Pop () |> ignore }

interface ILogProvider with

member this.GetLogger(name: string): Logger =
writeMessage name
member this.OpenMappedContext(arg1: string) (arg2: obj) (arg3: bool): System.IDisposable =
failwith "Not Implemented"
member this.OpenNestedContext(arg1: string): System.IDisposable =
failwith "Not Implemented"
member this.OpenMappedContext(key: string) (value: obj) (destructure: bool): System.IDisposable =
addProp key value
member this.OpenNestedContext(message: string): System.IDisposable =
addProp "NDC" message

let create () =
ConsoleProvider () :> ILogProvider
Expand Down