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

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

Open EGDevOpsTeam opened this issue 1 year ago • 1 comments

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:

  • Properties that are named just like meta data fields also override them and don't add them as a new property;
  • Allow placeholders in appsettings.json that are resolved on startup - like in log templates on event emit;
    • e.g.: host: "{machineName}"
  • Allow in a simple way to extend the already load appsettings.json configuration via code on logger creation;

EGDevOpsTeam avatar Jul 03 '23 17:07 EGDevOpsTeam

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?

HaGGi13 avatar Jul 03 '23 17:07 HaGGi13