I have the following appsettings.json configuration:
{
"Serilog": {
"Using": [],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"Enrich": [ "FromLogContext", "WithMachineName" ],
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "C:\\Logs\\log.json",
"formatter": "Serilog.Formatting.Elasticsearch.ElasticsearchJsonFormatter, Serilog.Formatting.Elasticsearch"
}
}
]
}
}
What I am trying to configure in the above appsettings.json file would be represented in C# as something like this:
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.WriteTo.File(new ElasticsearchJsonFormatter(inlineFields:true, renderMessageTemplate: false), @"C:\logs\log.json")
.CreateLogger();
I need to set "inlineFields" equal to true and "renderMessageTemplate" equal to false as overrides in my appsettings.json file on the ElasticsearchJsonFormatter instance. Is there a way to do that in the appsettings.json file so that I can keep my configuration out of C#?
Integrating Serilog logger into an ASP.NET Core application is very simple. You just need to follow the following steps: Configure Serilog as a Logging provider in the .NET Core host or, register Serilog with the Logger service during startup
Serilog’s built-in JsonFormatter implements this, but to my eyes, its output is awkward and verbose. The newer CompactJsonFormatter and RenderedCompactJsonFormatter in Serilog.Formatting.Compact produce cleaner JSON, and that format is supported by other tools in the Serilog ecosystem and beyond, so they’re a better starting point.
2) If we want to use text formatting instead of JSON formatting, the log message can be formatted using “outputTemplate” section inside file section like the following :
Configuring Serilog to output to the console is literally a one liner in Program.cs: Log.Logger = new LoggerConfiguration() .WriteTo.Console() .CreateLogger(); Customize Log message format. Each logger has a default template for the message that is to be logged.
I asked this question on GitHub: https://github.com/serilog/serilog-sinks-file/issues/138
No way :(
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With