serilog-sinks-elasticsearch
serilog-sinks-elasticsearch copied to clipboard
appsettings.json equivalent to ElasticsearchJsonFormatter()
I am using Serilog with a .NET Core 3.1 web application, but I am configuring Serilog via the appsettings.json; however, I am trying to establish how to apply ElasticsearchJsonFormatter() to the "formatter" field of the appsettings.json
{
"AllowedHosts": "*",
"Serilog": {
"Using": ["Serilog.Sinks.Console"],
"MinimumLevel": {
"Default": "Verbose",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"WriteTo": [
{
"Name": "Console",
"Args": {
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
}
},
]
}
}
As you can see I have the standard json formatter, but I don't know what to put for the ElasticsearchJsonFormatter(). Is this even possible, or do I have to reconfigure Serilog to not use appsettings.json
Did you ever find a solution for this ?
Hi, I installed the nuget package Serilog.Sinks.Elasticsearch and chaged the formatter option in the appsettings.json to Serilog.Formatting.Elasticsearch.ElasticsearchJsonFormatter,Serilog.Formatting.Elasticsearch
{
"AllowedHosts": "*",
"Serilog": {
"Using": ["Serilog.Sinks.Console"],
"MinimumLevel": {
"Default": "Verbose",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"WriteTo": [
{
"Name": "Console",
"Args": {
"formatter": "Serilog.Formatting.Elasticsearch.ElasticsearchJsonFormatter,Serilog.Formatting.Elasticsearch"
}
},
]
}
}
Hi all, This seems an old thread and currently it doesn't directly related with this repo.
But just want to check if it is possible to set formatProvider in Console with appSettings.json settings. Currently I also tried the previous(☝) post's suggestion, but it doesn't work. Just code-behind setting works. If there is any other solution, would be happy to have.
Hi @ardacetinkaya . My suggestion is to rule out one of these two problems first.
- Serilog is loading its configuration from
appsettings.json? - Is any component failing to load?
To rule out both of these problems, you need to modify Program.cs in the host project.
Some things to check
-
Line 5: Log into
Console.Error(stderronlinux) if anything goes wrong with Serilog -
Line 11 :
CreateDefaultBuilderis adding configuration sources ,appsetting.jsonis among them. If you are using something else be sure to addappsettings.jsonas a configuration source -
Line 14:
logConfig.ReadFrom.Configuration(ctx.Configuration)loads the Serilog configuration the app's configuration that includesappsettings.json(because of line 11)
This is a small example in .netcore 3.1
public class Program
{
public static void Main(string[] args)
{
Serilog.Debugging.SelfLog.Enable(System.Console.Error);
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host
.CreateDefaultBuilder(args)
.UseSerilog((ctx, logConfig) =>
{
logConfig.ReadFrom.Configuration(ctx.Configuration);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
Hope this helps!!!
@yuudj Thanks it worked