serilog-sinks-elasticsearch icon indicating copy to clipboard operation
serilog-sinks-elasticsearch copied to clipboard

appsettings.json equivalent to ElasticsearchJsonFormatter()

Open daz1761 opened this issue 5 years ago • 5 comments
trafficstars

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

daz1761 avatar Feb 07 '20 15:02 daz1761

Did you ever find a solution for this ?

Vandersteen avatar Sep 18 '20 13:09 Vandersteen

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"
        }
      },
    ]
  }
}

yuudj avatar Dec 01 '20 19:12 yuudj

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.

ardacetinkaya avatar May 30 '22 07:05 ardacetinkaya

Hi @ardacetinkaya . My suggestion is to rule out one of these two problems first.

  1. Serilog is loading its configuration from appsettings.json ?
  2. 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 (stderr on linux) if anything goes wrong with Serilog

  • Line 11 : CreateDefaultBuilder is adding configuration sources , appsetting.json is among them. If you are using something else be sure to add appsettings.json as a configuration source

  • Line 14: logConfig.ReadFrom.Configuration(ctx.Configuration) loads the Serilog configuration the app's configuration that includes appsettings.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 avatar May 30 '22 13:05 yuudj

@yuudj Thanks it worked

sumeshes07 avatar Aug 23 '23 13:08 sumeshes07