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

Configure source and jsonFormatter in appsettings.json #156

Closed agilenut closed 2 years ago

agilenut commented 2 years ago

I'd like to configure the source and a custom jsonFormatter, such as the CompactSplunkJsonFormatter, in appsettings.json. I've been able to do one or the other but not both. I'm assuming this is because the overloads that accept a jsonFormatter expect you to pass the source into the formatter instead of into the sink. Is there some kind of appsettings.json syntax to make this work? If not, is there some enhancement you can think of that would make this work?

agilenut commented 2 years ago

I figured it out.

I had tried something like this earlier based on this documentation

{
    "Serilog": {
        "WriteTo": [{
            "Name": "EventCollector",
            "Args": {
                "splunkHost": "https://my-host.splunkcloud.com",
                "eventCollectorToken": "my-token",
                "jsonFormatter": {
                    "type": "Serilog.Sinks.Splunk.CompactSplunkJsonFormatter, Serilog.Sinks.Splunk",
                    "source": "my-source"
                } 
            }
        }]
    }
}

It turns out I had to update Serilog.Settings.Configuration to 3.3.0. I was using 3.1.0 because that is what was pulled in by Serilog.AspNetCore 4.1.0 (currently latest stable).

Maybe this helps someone else.