Vish
Vish

Reputation: 384

How to use Serilog configuration from azure app setting or a file for azure function app?

I am trying to add serilog to my azure function application. But I am not able to read serilog configuration from local.settings.json and I am not finding a way to get serilog configuration azure application setting.

I like to read setting from configuration because my serilog settings are environment/platform specific.

I have added below code in my startup.cs

    var config = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("local.settings.json", true, true)
        .AddEnvironmentVariables()
        .Build();

var logger = new LoggerConfiguration()
               .ReadFrom.Configuration(config)
               .CreateLogger();

in local.settings.jsonI have serilog config details.

"Serilog": {
    "MinimumLevel": {
      "Default": "Verbose"
    },
    "WriteTo": [
      {
        "Name": "Seq",
        "Args": { "serverUrl": "http://DEV01:5341" }
      },
      {
        "Name": "Logentries",
        "Args": { "Token": "ABCDXYZ" }
      },
      {
        "Name": "Console"
      }
    ]
  }

Thank you.

Upvotes: 4

Views: 2769

Answers (1)

Tony Ju
Tony Ju

Reputation: 15609

Application settings in Azure portal or values in local.settings.json only accept Dictionary<string, string> format. If you use settings with more than one level in local.settings.json, it will ignore the whole "Values" section. It should be something like

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "key1":"value1"
  }
}

In Azure portal, also only key-value format.

enter image description here

You can get these settings in the environment variables in this way

Environment.GetEnvironmentVariable("key")

So the workarounds are

1.you read and parse the json file.

2.use : to nest:keys:down

Reference:

Azure Function Read local.settings.json to object

azure application settings - how to add nested item

Upvotes: 3

Related Questions