Skip to content

Instantly share code, notes, and snippets.

@jshurst
Last active August 27, 2022 18:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jshurst/e579f7f1fbe1a9a9e843 to your computer and use it in GitHub Desktop.
Save jshurst/e579f7f1fbe1a9a9e843 to your computer and use it in GitHub Desktop.
F# Agent Logger
namespace FSharpRulesEngine
open System
type LogMessageType =
| Information
| Exception
type ILogger =
abstract Log : LogMessageType * string -> unit
abstract FetchLog : unit -> (LogMessageType * string) []
abstract Die : unit -> unit
abstract Reset : unit -> unit
type LogMsg =
| Die
| Reset
| LogMessage of (LogMessageType * string)
| FetchLog of AsyncReplyChannel<(LogMessageType * string)[]>
type Logger() =
let innerAccumulator =
MailboxProcessor.Start(fun inbox ->
let rec loop n =
async { let! msg = inbox.Receive()
match msg with
| Die -> return ()
| Reset -> return! loop([])
| LogMessage x -> return! loop(x::n)
| FetchLog(reply) ->
let rev = (List.rev n) |> List.toArray
reply.Reply(rev);
return! loop n }
loop [])
interface ILogger with
member this.Log(msgType, msg) = innerAccumulator.Post(LogMessage (msgType, msg))
member this.FetchLog() = innerAccumulator.PostAndReply((fun reply -> FetchLog(reply)))//, timeout = 2000)
member this.Die() = innerAccumulator.Post(Die)
member this.Reset() = innerAccumulator.Post(Reset)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment