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

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

Open
EGDevOpsTeam opened this issue Jul 3, 2023 · 1 comment

Comments

@EGDevOpsTeam
Copy link

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;
@HaGGi13
Copy link

HaGGi13 commented Jul 3, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants