I don't want to add StreamWriter
parameter to Write-To-File procedure but when I'm trying to work with disposable StreamWriter there I'm getting:
An unhandled exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll
Additional information: Cannot write to a closed TextWriter.
code:
let fileLogger = {
new IFlog with
member i.FLog level format =
use file = LogFile()
Printf.kfprintf
(fun f ->
fprintfn file "[%s][%A] "
<| level.ToString()
<| DateTime.Now
) file (format)
so when I call FLog method twice I get this.
I can use it alike this : member i.FLog file level format
and control disposable object on top level but then I lose all the abstraction.
Is there some way to use disposable objects like that? Or how can I change architecture to avoid passing disposable parameter into this function?
LogFile is :
let mutable LogFileName = "F.log"
let LogFile() =
match File.Exists(LogFileName) with
| false -> File.CreateText(LogFileName)
| true -> File.AppendText(LogFileName)