printf style logging for f#

后端 未结 3 466
滥情空心
滥情空心 2020-12-21 08:41

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 InfoFo

3条回答
  •  囚心锁ツ
    2020-12-21 08:50

    You can use standard logging subsystem that defined in System.Diagnostic namespace. You shall be sure that your logging enviromnet correctly initialized. For example something like this (part of example in C#) but it easy is linked with f# code.

    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");
    

    so in f#

    open System
    open System.Diagnostics
    module ClientConsole =
        let Run _ =
          Trace.TraceInformation("Client started");
    

    For more convenient you can use another trace listener that definded by third party programmer. For example lool at : AlignedTextWriterTraceListener

提交回复
热议问题