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
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()
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}
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse