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 for Sink config from appsettings.json #87

Closed alastairtree closed 6 years ago

alastairtree commented 6 years ago

Hello - thanks for the library.

I was wondering if you support reading the sink config from the configuration files using Serilog.Settings.Configuration? I can't seem to get the code below to add the sink? Am I doing it wrong or is it not supported? Or could you point me to some docs?

appsettings.json

"WriteTo": [
      {
          "Name": "EventCollector",
          "Args": {
             "splunkHost": "https://myhost/services/collector",
             "eventCollectorToken": "0000"
          }
       }
    ],

program.cs

Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(configuration)
                .CreateLogger();
merbla commented 6 years ago

@alastairtree there is a working version of this in #92.

What you have above seems correct. Make sure that your appsetting.json is available

var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json")
    .Build();

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    .CreateLogger();
{
    "Serilog": {
        "Using": ["Serilog.Sinks.Console", "Serilog.Sinks.Splunk"],
        "MinimumLevel": "Information",
        "WriteTo": [{
                "Name": "Console"
            },
            {
                "Name": "EventCollector",
                "Args": {
                    "splunkHost": "http://splunk:8088",
                    "eventCollectorToken": "00112233-4455-6677-8899-AABBCCDDEEFF"
                }
            }
        ],
        "Properties": {
            "Application": "Serilog Splunk Console Sample",
            "Serilog.Sinks.Splunk.Sample": "ViaEventCollector",
            "Serilog.Sinks.Splunk.Sample.TestType": "AppSettings.json"
        }
    }
}