DEV Community

Rob Mulpeter
Rob Mulpeter

Posted on • Edited on

Configure and implement Serilog into F#

The purpose of this article is to demonstrate the code required to implement the Serilog Log into an F# console application.

Setup

The below example was built into a Console Application using F# on the .NET 6 Framework. The following packages were installed:

Serilog/2.11.0
Serilog.Sinks.Console/4.0.1
Serilog.Formatting.Compact/1.1.0
Serilog.Enrichers.Environment/2.2.0
Serilog.Enrichers.Process/2.0.2
Serilog.Enrichers.Span/2.3.0
Serilog.Enrichers.Thread/3.1.0
Enter fullscreen mode Exit fullscreen mode

Code

Copy the below code into your Progam.fs file and run the application. This will print 5 logs to your terminal. The LoggerConfiguration makes use of several enrichers to add additional data to the output as well as using RenderedCompactJsonFormatter which instructs the console writer to output the message in json format.

open System
open Serilog
open Serilog.Formatting.Compact

type Data = {
    Id: int
    On: bool
    Created: DateTime
    Message: string
    MessageSome: string option
    MessageNone: string option
}

[<EntryPoint>]
let main argv =

  Log.Logger <-
    LoggerConfiguration()
      .MinimumLevel.Debug()
      .Enrich.FromLogContext()
      .Enrich.WithEnvironmentName()
      .Enrich.WithMachineName()
      .Enrich.WithThreadId()
      .Enrich.WithProcessId()
      .WriteTo.Console(RenderedCompactJsonFormatter())
      .CreateLogger()

  let dummyData : Data = {
        Id = 1
        On = true
        Created = DateTime.UtcNow
        Message = "Content"
        MessageSome = Some "Content"
        MessageNone = None    
  }

  try
      try          
          Log.Logger.Debug("{dummyData}", dummyData)
          Log.Logger.Information("{dummyData}", dummyData)      
          Log.Logger.Warning("{dummyData}", dummyData)
          Log.Logger.Error("{dummyData}", dummyData)
          Log.Logger.Fatal("{dummyData}", dummyData)

          1 / 0
      with
         | ex ->
             Log.Error($"Error: %s{ex.Message}")
             1
  finally
      Log.CloseAndFlush()
Enter fullscreen mode Exit fullscreen mode

Output

{"@t":"2022-07-08T07:49:44.0016610Z","@m":"\"{ Id = 1\n  On = true\n  Created = 07/08/2022 07:49:44\n  Message = \\\"Content\\\"\n  MessageSome = Some \\\"Content\\\"\n  MessageNone = None }\"","@i":"60a52653","@l":"Debug","dummyData":"{ Id = 1\n  On = true\n  Created = 07/08/2022 07:49:44\n  Message = \"Content\"\n  MessageSome = Some \"Content\"\n  MessageNone = None }","EnvironmentName":"Production","MachineName":"X-MAC","ThreadId":1,"ProcessId":69944}
{"@t":"2022-07-08T07:49:44.1605920Z","@m":"\"{ Id = 1\n  On = true\n  Created = 07/08/2022 07:49:44\n  Message = \\\"Content\\\"\n  MessageSome = Some \\\"Content\\\"\n  MessageNone = None }\"","@i":"60a52653","dummyData":"{ Id = 1\n  On = true\n  Created = 07/08/2022 07:49:44\n  Message = \"Content\"\n  MessageSome = Some \"Content\"\n  MessageNone = None }","EnvironmentName":"Production","MachineName":"X-MAC","ThreadId":1,"ProcessId":69944}
{"@t":"2022-07-08T07:49:44.1611770Z","@m":"\"{ Id = 1\n  On = true\n  Created = 07/08/2022 07:49:44\n  Message = \\\"Content\\\"\n  MessageSome = Some \\\"Content\\\"\n  MessageNone = None }\"","@i":"60a52653","@l":"Warning","dummyData":"{ Id = 1\n  On = true\n  Created = 07/08/2022 07:49:44\n  Message = \"Content\"\n  MessageSome = Some \"Content\"\n  MessageNone = None }","EnvironmentName":"Production","MachineName":"X-MAC","ThreadId":1,"ProcessId":69944}
{"@t":"2022-07-08T07:49:44.1615460Z","@m":"\"{ Id = 1\n  On = true\n  Created = 07/08/2022 07:49:44\n  Message = \\\"Content\\\"\n  MessageSome = Some \\\"Content\\\"\n  MessageNone = None }\"","@i":"60a52653","@l":"Error","dummyData":"{ Id = 1\n  On = true\n  Created = 07/08/2022 07:49:44\n  Message = \"Content\"\n  MessageSome = Some \"Content\"\n  MessageNone = None }","EnvironmentName":"Production","MachineName":"X-MAC","ThreadId":1,"ProcessId":69944}
{"@t":"2022-07-08T07:49:44.1750560Z","@m":"\"{ Id = 1\n  On = true\n  Created = 07/08/2022 07:49:44\n  Message = \\\"Content\\\"\n  MessageSome = Some \\\"Content\\\"\n  MessageNone = None }\"","@i":"60a52653","@l":"Fatal","dummyData":"{ Id = 1\n  On = true\n  Created = 07/08/2022 07:49:44\n  Message = \"Content\"\n  MessageSome = Some \"Content\"\n  MessageNone = None }","EnvironmentName":"Production","MachineName":"X-MAC","ThreadId":1,"ProcessId":69944}
{"@t":"2022-07-08T07:49:44.1825110Z","@m":"Error: Attempted to divide by zero.","@i":"79a82f6d","@l":"Error","EnvironmentName":"Production","MachineName":"X-MAC","ThreadId":1,"ProcessId":69944}


Enter fullscreen mode Exit fullscreen mode

Top comments (0)