Serilog를 F#으로 구성 및 구현

이 문서의 목적은 Serilog 로그를 F# 콘솔 애플리케이션에 구현하는 데 필요한 코드를 보여 주는 것입니다.

설정

아래 예제는 Console Application 에서 F# 를 사용하여 .NET 6 Framework 에 빌드되었습니다. 다음 패키지가 설치되었습니다.

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


암호

아래 코드를 Progam.fs 파일에 복사하고 애플리케이션을 실행합니다. 이렇게 하면 터미널에 5개의 로그가 인쇄됩니다. LoggerConfiguration는 콘솔 작성자에게 메시지를 json 형식으로 출력하도록 지시하는 RenderedCompactJsonFormatter를 사용할 뿐만 아니라 출력에 추가 데이터를 추가하기 위해 여러 보강자를 사용합니다.

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()


산출

{"@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}


좋은 웹페이지 즐겨찾기