Skip to content

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

Open
@EGDevOpsTeam

Description

@EGDevOpsTeam

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;

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions