Structured configuration variables in json array

Given the following appsettings.json:

{
    "Serilog" : {
        "WriteTo" : [
            {
                "Name" : "File",
                "Args" : {
                    "Path" : "Logs/Log.txt"
                }
            },
            {
                "Name" : "ApplicationInsights",
                "Args" : {
                    "InstrumentationKey" : "xxxxxxxx"
                }
            }
        ]
    }
}

How would I go about replacing “InstrumentationKey” using structured configuration variable substitution? I’ve tried:

  • Serilog:WriteTo[@Name=“ApplicationInsights”]:Args:instrumentationKey
  • Serilog:WriteTo[Name=“ApplicationInsights”]:Args:instrumentationKey
  • Serilog:WriteTo[“ApplicationInsights”]:Args:instrumentationKey
  • Serilog:WriteTo[1]:Args:instrumentationKey

… without success.

Hi @niklas.uhlin

Thanks for your question!

I think to get the instrumentationKey you’d need a variable with a name like this:

Serilog:WriteTo:1:Args:InstrumentationKey

The key part is the ordinal index doesn’t need square brackets.

I ran this through a quick test and it seemed to work for me.

Hope that helps!

Best,

2 Likes

Worked like a charm.
Thanks!

Hey @niklas.uhlin

Awesome, glad to hear it worked!

Have a great rest of your day!

Best,