DataDog / serilog-sinks-datadog-logs

Serilog Sink that sends log events to Datadog https://www.datadoghq.com/
Apache License 2.0
60 stars 41 forks source link

Support for merging appsettings with DatadogLogs LoggerSinkConfiguration extension #85

Closed upnxt closed 1 year ago

upnxt commented 1 year ago

Currently it appears the DataDog sink must be configured via appsettings or .WriteTo.DatadogLogs(...) without a way to overlap the two configuration methods. For those of us that want to configure the sink via appsettings, but do not want to source control our API key this poses a problem.

This PR will merge appsetting values with the hard-coded DatadogLogs parameters making a combined configuration possible.

"Serilog": {
    "Using": [ "Serilog.Sinks.Datadog.Logs" ],
    "WriteTo": [
        {
            "Name": "DatadogLogs",
            "Args": {
                "source": "application",
                "service": "web-api",
                "tags": [
                    "tag1:v1",
                    "tag2:v2",
                ],
                "batchSizeLimit": 100,
                "batchPeriod": "00:00:01",
                "queueLimit": 10000
            }
        }
    ]
}
var logger = new LoggerConfiguration()
    .ReadFrom.Configuration(builder)
    .WriteTo.DatadogLogs(apiKey: loggingApiKey, sinkConfigurationSection: builder.GetSection("Serilog").GetSection("WriteTo").GetChildren().FirstOrDefault().GetSection("Args"))
    .CreateLogger();
upnxt commented 1 year ago

@ogaca-dd, @gh123man is there an ETA on when this may be reviewed?

gh123man commented 1 year ago

@upnxt Thanks for the PR. I understand your use case - but have you considered overriding the configuration before initializing the Sink? You can do this today with IConfigurationRoot without making an api surface change to the sink.

var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true)
    .Build();

// Something like this
configuration["Serilog:WriteTo:0:Args:apiKey"] = "SOME_API_KEY";

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    ...
upnxt commented 1 year ago

@upnxt Thanks for the PR. I understand your use case - but have you considered overriding the configuration before initializing the Sink? You can do this today with IConfigurationRoot without making an api surface change to the sink.

var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true)
    .Build();

// Something like this
configuration["Serilog:WriteTo:0:Args:apiKey"] = "SOME_API_KEY";

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    ...

Hah, times like these are a bit embarrassing when completely overlooking the most basic fix. Thanks for the reminder, I'll close this out!