Last active
August 27, 2022 18:40
-
-
Save jshurst/e579f7f1fbe1a9a9e843 to your computer and use it in GitHub Desktop.
F# Agent Logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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