f# 的 printf 样式日志记录 [英] printf style logging for f#
问题描述
如何使用类似于 log4net 的日志库为 f# 设置 printf 样式的记录器.我有 Log.Debug、Info、Warn 等类似于 log4net 中的 DebugFormat 或 InfoFormat 的函数.我试图为我的 Log 类设置类型扩展,我可以像 Log.Debugf "%s" "foo" 这样的 printf 样式调用它.我的通用日志函数如下所示:
How do i setup a printf-style logger for f# using logging library similar to log4net. i have Log.Debug, Info, Warn, etc. functions that are similar to DebugFormat or InfoFormat in log4net. i tried to setup type extensions for my Log class that i could call in printf style like Log.Debugf "%s" "foo". my generic log function looks like this:
let log format = Printf.kprintf (sprintf "%s") format
我在使用扩展函数签名登录到我的调试函数时遇到问题...我尝试使用 Debugf 格式和 Debug
i am having trouble with the extension function signature to log to my Debug function... i tried using Debugf format and Debug
推荐答案
我不熟悉 log4net,但假设您正在登录 MessageBox(就像专业人士一样),您可以执行以下操作:
I'm not familiar with log4net, but assuming you're logging to a MessageBox (like the pros do), you can do the following:
let log format = Printf.kprintf (fun msg -> System.Windows.Forms.MessageBox.Show(msg)) format
在这种情况下,由于Show
需要一个字符串,它可以缩短为:
In this case, since Show
takes a string, it can be shortened to:
let log format = Printf.kprintf System.Windows.Forms.MessageBox.Show format
这篇关于f# 的 printf 样式日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!