Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nenadvicentic opened this issue Nov 8, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@nenadvicentic
Copy link

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 nenadvicentic added the enhancement New feature or request label Nov 8, 2023
@haggerty-william
Copy link

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
Copy link

gonace commented Jan 30, 2024

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

@MithrilMan
Copy link

This is really something essential for us too.

@MithrilMan
Copy link

MithrilMan commented Feb 12, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants