ecs-dotnet icon indicating copy to clipboard operation
ecs-dotnet copied to clipboard

[FEATURE] Enable full configuration of `Elastic.Serilog.Sinks` through `appsettings.json`

Open nenadvicentic opened this issue 2 years ago • 8 comments

Serilog supports full configuration of logging through appsettings.json via Serilog.Settings.Configuration NuGet package (project is here)

For example Serilog.Sinks.Elasticsearch supports full configuration via appsettings.json. This would be an example:

{
  "Serilog": {
    "Using": [ "Serilog.Sinks.Elasticsearch" ],
    "MinimumLevel": "Warning,
    "WriteTo": [
      {
        "Name": "Elasticsearch",
        "Args": {
          "nodeUris": "http://localhost:9200",
          "inlineFields": true
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName" ],
    "Properties": {
      "Application": "My app"
    }
  },
}

In Serilog.Sinks.Elasticsearch this is achieved by exposing extension method for configuration of each of input parameters. Int the code example bellow is simplified extension method, showing only two "Args" parameters used in above JSON configuration file.

        public static LoggerConfiguration Elasticsearch(
            this LoggerSinkConfiguration loggerSinkConfiguration,
            string nodeUris,
            // ...
            bool inlineFields = false,
            // ...
            )

Full source code of the above extension method can be found here

nenadvicentic avatar Nov 08 '23 11:11 nenadvicentic

It seems like there is some support. I can tell at least that it will try to ship logs to elastic with default options if you include a Elasticsearch section in the WriteTo array. Is there anyway to tell what is currently supported?

haggerty-william avatar Dec 21 '23 14:12 haggerty-william

This is the only reason why we're not moving to Elastic.Serilog.Sinks, so we're waitng for this implementation 🎉

gonace avatar Jan 30 '24 09:01 gonace

This is really something essential for us too.

MithrilMan avatar Feb 11 '24 21:02 MithrilMan

fyi I've implemented a workaround: create a custom extension that works with appsettings.json investigating the code on https://github.com/serilog/serilog-settings-configuration I've found a way to implement my own extension method that works with my settings, it requires to:

  • create an extension method that's compatible with the serilog.settings.configuration alghoritm that looks for extension method to call
  • insert it in a proper project whose name contains the "Serilog" word, or add "Using" property under Serilog settings in appsettings.json

In my case, the need was just to configure the endpoint address. Since I was coming from serilog-sinks-elasticsearch package that had a nodeUris property, I basically just implemented this extension

using Elastic.Serilog.Sinks;
using Serilog.Configuration;

namespace WAY.BuildingBlocks.API;

public static class LoggerConfigurationWayExtensions
{
   public static LoggerConfiguration Elasticsearch(this LoggerSinkConfiguration loggerConfiguration, string nodeUris)
      => loggerConfiguration.Elasticsearch(nodes: [new Uri(nodeUris)]);
}

The discovery process of serilog, among other peculiarities, involves the extension be named like the sink (so Elasticsearch in this example) and be implemented in a project whose name contains "Serilog" OR the assembly name be included in the "Using" property under "Serilog" settings ( see https://github.com/serilog/serilog-settings-configuration?tab=readme-ov-file#using-section-and-auto-discovery-of-configuration-assemblies)

Since my project haven't Serilog word in it, I used the "Using" like this image

In appsettings I had just to add nodeUris under "Args" property

"WriteTo": [
   {
      "Name": "Elasticsearch",
      "Args": {
         "nodeUris": "http://logger-elasticsearch:9200"
      }
   }
]

and now my extension gets called, that just calls the original extension method accepting an Uri enumeration.

So, while we wait for an official solution, we can create our own extension that fit our needs

MithrilMan avatar Feb 12 '24 13:02 MithrilMan