I am trying to synchronize logging to a file in an F Sharp project. Using the lock computational expression I tried to approximate a resource lock, however it seems to not be working.
module regiondeployer.logger
open System
open System.IO
open Microsoft.FSharp.Core
open regiondeployer.personalprojectroot
type private logginglock =
static member public lock = new Object()
[<Literal>]
let private logfile = personalprojectroot + "log.txt"
let public initialize() : unit =
use init = File.Create(logfile)
()
let public logtoconsoleandfile (message:string) : unit =
lock logginglock.lock (fun _ ->
Console.WriteLine message
use logfilestream = File.AppendText(logfile)
logfilestream.WriteLine(message)
)
System.IO.IOException HResult=0x80070020 Message=The process cannot access the file 'log.txt' because it is being used by another process. Source=mscorlib
What am I missing?