F# f的printf样式日志记录#

F# f的printf样式日志记录#,f#,F#,如何使用类似于log4net的日志库为f#设置printf样式的记录器。 我有Log.Debug、Info、Warn等函数,这些函数类似于log4net中的DebugFormat或InfoFormat。我试图为我的日志类设置类型扩展,我可以像Log.Debugf“%s”foo那样以printf样式调用它。我的通用日志函数如下所示: let log format = Printf.kprintf (sprintf "%s") format 我在登录到调试函数的扩展函数签名方面遇到问题。。。 我尝

如何使用类似于log4net的日志库为f#设置printf样式的记录器。 我有Log.Debug、Info、Warn等函数,这些函数类似于log4net中的DebugFormat或InfoFormat。我试图为我的日志类设置类型扩展,我可以像Log.Debugf“%s”foo那样以printf样式调用它。我的通用日志函数如下所示:

let log format = Printf.kprintf (sprintf "%s") format
我在登录到调试函数的扩展函数签名方面遇到问题。。。
我尝试使用Debugf格式和Debug

我不熟悉log4net,但假设您正在登录到MessageBox(与pros一样),您可以执行以下操作:

let log format = Printf.kprintf (fun msg -> System.Windows.Forms.MessageBox.Show(msg)) format
在这种情况下,由于
Show
采用字符串,因此可以将其缩短为:

let log format = Printf.kprintf System.Windows.Forms.MessageBox.Show format

你是说像这样的

open System

type SomeLogger() = 
    member this.Error(format : string, [<ParamArray>]args : obj[] ) = ()
    member this.Info(format : string, [<ParamArray>]args : obj[] ) = ()


module Extensions = 
    type SomeLogger with
        member this.FInfo format = Printf.ksprintf (this.Info) format
        member this.FError format = Printf.ksprintf (this.Error) format

open Extensions

let l = new SomeLogger()
l.FInfo "%d%s" 10 "123"
开放系统
键入SomeLogger()=
此成员。错误(格式:字符串,[]参数:obj[])=()
member this.Info(格式:string,[]args:obj[])=()
模块扩展=
键入带有
成员this.FInfo format=Printf.ksprintf(this.Info)格式
成员this.FError format=Printf.ksprintf(this.Error)格式
开放扩展
设l=newsomelogger()
l、 FInfo“%d%s”10“123”

您可以使用System.Diagnostic命名空间中定义的标准日志子系统。 您应确保您的日志环境已正确初始化。例如,类似这样的东西(C#中示例的一部分),但它很容易与f#代码链接

所以在f中#

为了更方便,您可以使用另一个由第三方程序员定义的跟踪侦听器。
例如lool at:

多亏了你们两位,我绕着这个跳了一会儿,最后放弃了(不寻常的*:)
Trace.Listeners.Clear();
try {
    TextWriterTraceListener infoTextLogger = new AlignedTextWriterTraceListener(@"c:\temp\log.log");
    infoTextLogger.Filter = new EventTypeFilter(SourceLevels.All);
    infoTextLogger.TraceOutputOptions = TraceOptions.DateTime | TraceOptions.ProcessId | TraceOptions.ThreadId;
    Trace.Listeners.Add(infoTextLogger);
    TextWriterTraceListener consoleWriter = new AlignedTextWriterTraceListener(System.Console.Out);
    consoleWriter.Filter = new EventTypeFilter(SourceLevels.Information);
    Trace.Listeners.Add(consoleWriter);
} catch (Exception exp) {
    throw exp;
}
AlignedTextWriterTraceListener.TraceSourceNameLength = SOURCE_NAME_FIELD_LENGTH;
Trace.AutoFlush = true;
Trace.TraceInformation("Logging subsystem has been initiated");
open System
open System.Diagnostics
module ClientConsole =
    let Run _ =
      Trace.TraceInformation("Client started");