Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Rob
Rob

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 aConsole Application usingF# on the.NET 6 Framework. The following packages were installed:

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

Code

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

openSystemopenSerilogopenSerilog.Formatting.CompacttypeData={Id:intOn:boolCreated:DateTimeMessage:stringMessageSome:stringoptionMessageNone:stringoption}[<EntryPoint>]letmainargv=Log.Logger<-LoggerConfiguration().MinimumLevel.Debug().Enrich.FromLogContext().Enrich.WithEnvironmentName().Enrich.WithMachineName().Enrich.WithThreadId().Enrich.WithProcessId().WriteTo.Console(RenderedCompactJsonFormatter()).CreateLogger()letdummyData:Data={Id=1On=trueCreated=DateTime.UtcNowMessage="Content"MessageSome=Some"Content"MessageNone=None}trytryLog.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/0with|ex->Log.Error($"Error: %s{ex.Message}")1finallyLog.CloseAndFlush()
Enter fullscreen modeExit fullscreen mode

Output

{"@t":"2022-07-08T07:49:44.0016610Z","@m":"\"{Id =1\nOn =true\nCreated =07/08/202207:49:44\nMessage = \\\"Content\\\"\nMessageSome =Some \\\"Content\\\"\nMessageNone =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\nOn =true\nCreated =07/08/202207:49:44\nMessage = \\\"Content\\\"\nMessageSome =Some \\\"Content\\\"\nMessageNone =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\nOn =true\nCreated =07/08/202207:49:44\nMessage = \\\"Content\\\"\nMessageSome =Some \\\"Content\\\"\nMessageNone =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\nOn =true\nCreated =07/08/202207:49:44\nMessage = \\\"Content\\\"\nMessageSome =Some \\\"Content\\\"\nMessageNone =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\nOn =true\nCreated =07/08/202207:49:44\nMessage = \\\"Content\\\"\nMessageSome =Some \\\"Content\\\"\nMessageNone =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 modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromRob

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp