elastic / ecs-dotnet

https://www.elastic.co/guide/en/ecs-logging/dotnet/current/setup.html
Apache License 2.0
114 stars 58 forks source link

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

Open nenadvicentic opened 10 months ago

nenadvicentic commented 10 months ago

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

haggerty-william commented 8 months ago

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?

gonace commented 7 months ago

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

MithrilMan commented 6 months ago

This is really something essential for us too.

MithrilMan commented 6 months ago

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:

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

aj-scram commented 3 months ago

My company is also looking to switch to elastic apm and this is a major issue for us as well

@MithrilMan : I've done the same for modifying mongo sink logging before, so I'm not uncomfortable with that solution. Appreciate you taking the time for looking into that and doing a write up 👍

MithrilMan commented 3 months ago

@aj-scram you are welcome, long live the art of making do! :)

ReinhardvdBerg commented 2 months ago

I'm trying to migrate lots of projects to Elastic.Serilog.Sinks and am a bit disappointed that out of the box appsettings.json config does not exist yet.

darianmh commented 2 months ago

I'm using this config and it works { "Name": "Elasticsearch", "Args": { "nodes": [ "http://elasticsearch:9200" ] } }

marcelo-g-simas commented 3 days ago

My company is also looking at migrating many systems to this package and this is a major blocker for us. I wonder how different the two code bases are, perhaps it would not be too much work to bring over that feature from the now deprecated sink.