serilog-contrib / serilog-sinks-splunk

A Serilog sink that writes to Splunk
https://splunk.com
Apache License 2.0
46 stars 47 forks source link

Support set meta data field host easily by code (dynamic resolution) #165

Open EGDevOpsTeam opened 1 year ago

EGDevOpsTeam commented 1 year ago

Hello,

This is a suggestion for improvement.

I have spent a few hours trying to find a solution to configure the Splunk EventCollector configuration in the appsettings.json file and resolve and set the host of the application on startup. Sadly I was not able to find an elegant solution.

Approach

Unfortunately I have not found a way that allows a combination of both configuration options. Except that the EventCollector is completely redefined via code and the appsettings.json has to be accessed pretty statically. Surely this can be done a bit more elegantly, but it was just for testing here.

Configuration (just an extraction of it):

  "Serilog": {
    "WriteTo:SplunkLogger": {
      "Name": "Logger",
      "Args": {
        "configureLogger": {
          "WriteTo": [
            {
              "Name": "EventCollector",
              "Args": {
                "splunkHost": "my-splunk-eventcollector-endpoint",
                "eventCollectorToken": "my-hec-token",
                "index": "my-index"
              }
            }
          ]
        }
      }
    }
  }

Quiet ugly code example:

var logger = new LoggerConfiguration()
    .ReadFrom.Configuration(Configuration)
    .WriteTo.EventCollector(
        splunkHost: Configuration["Serilog:WriteTo:SplunkLogger:Args:configureLogger:WriteTo:0:Args:splunkHost"],
        eventCollectorToken: Configuration["Serilog:WriteTo:SplunkLogger:Args:configureLogger:WriteTo:0:Args:eventCollectorToken"],
        index: Configuration["Serilog:WriteTo:SplunkLogger:Args:configureLogger:WriteTo:0:Args:index"],
        host: System.Environment.MachineName
    )
    .CreateLogger();

The before described code + configuration does the trick. The meta data field host is set properly with the machine name of the application's host. But it's more cumbersome than handy.

Suggestion

Therefore my suggestions for improvement:

HaGGi13 commented 1 year ago

Sorry, I used the wrong account to create this item. May I ask to delete it, so that I can re-create it with the correct account?