我有登录控制台的功能
Printf.kprintf
(printfn
"[%s][%A] %s"
<| level.ToString()
<| DateTime.Now)
format // fprint to System.Console.Out maybe
但它使用 Printf.StringFormat 作为格式,现在我想遵循相同的逻辑并将其打印到文件中。
所以我尝试
Printf.kfprintf
(fun f ->
fprintfn file "[%s][%A] "
<| level.ToString()
<| DateTime.Now
) file (format)
还有两件事我无法理解。为什么有unit -> 'A
而不是string -> 'A
?我应该如何使用它?我可以StringFormat
在这里使用我的TextWriterFormat
吗?
另一个问题是,我继承了第一个片段format
,string -> 'Result
但在 kfprintf 中我不能这样做,因为在内容unit -> 'Result
之前出现了格式消息[x][x]
。我想我可以以某种方式继承format
,f
但我找不到很好的例子,我唯一找到的是 F# 编译器的一部分:
[<CompiledName("PrintFormatToTextWriter")>]
let fprintf (os: TextWriter) fmt = kfprintf (fun _ -> ()) os fmt
[<CompiledName("PrintFormatLineToTextWriter")>]
let fprintfn (os: TextWriter) fmt = kfprintf (fun _ -> os.WriteLine()) os fmt
但我怎么能用这个unit
?我如何在我的消息之后发布消息?