Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to set this Configuration key/value in Azure App Settings (or Environmental Vars?) #102

Closed
PureKrome opened this issue Apr 20, 2018 · 7 comments
Labels

Comments

@PureKrome
Copy link

Hi 👋

Give this serilog configuration k/v's .. can this be set in Azure's App Settings or in Environmental Variables (which is what Azure does anyways):

Trying to set that Logentries token value.
It's the array, that's confusing me...

e.g. appsettings.production.json

 "Serilog": {
    "WriteTo": [
      {
        "Name": "Console"
      },
      {
        "Name": "Logentries",
        "Args": {
          "token": "SOME SECRET HERE",
          "outputTemplate": "{Level:u3}: {Message:lj}{NewLine}{Exception}"
        }
      }
    ]  
  }
@nblumhardt
Copy link
Member

Hi @PureKrome 👋

Yes, it's possible, although unfortunately it's not an optimal process right now; I believe given your config above, the variable name will be something like: Serilog:WriteTo:1:Args:token where the 1 is an index into the WRITETO array. May need some exploration; I seem to recall there's an example out there somewhere. HTH!

@lucasmaj
Copy link

lucasmaj commented Dec 4, 2019

I have similar need for AzureBlobStorage. Details here: chriswill/serilog-sinks-azureblobstorage#47

I ended up with something similar to what is described here and then extended is some.

Basically I build the configuration twice. After first build of configuration I AddInMemoryCollection to overwrite the secrets/connectionstrings and use the second config to configure the logger.

Most importantly, I keep the paths ("Serilog:WriteTo:1:Args:connectionString") in appsettings.json and used that to know which keys to add in AddInMemoryCollection.

 var configurationBuilder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json", false, true)
                .AddEnvironmentVariables();

            IConfiguration configuration = configurationBuilder.Build();

            var pathsToInjectStorageConnectionString = configuration.GetSection("PathsToInjectStorageConnectionString").GetChildren().Select(section => section.Value);

            if (pathsToInjectStorageConnectionString.Any())
            {
                var storageConnectionString = new Dmp.Timelines.Backup(configuration["DmpAppStorageAccountName"], configuration["DmpAppStorageAccountKey"]).StorageAccount.ToString(true);
                configurationBuilder.AddInMemoryCollection(pathsToInjectStorageConnectionString.Select(path => new KeyValuePair<string, string>(path, storageConnectionString)));
                configuration = configurationBuilder.Build();
            }

            var loggerConfiguration = new LoggerConfiguration()
                .ReadFrom.Configuration(configuration);

@PureKrome
Copy link
Author

@lucasmaj why build the config 2x. I didn't have to ....

@lucasmaj
Copy link

lucasmaj commented Dec 5, 2019

I guess in your situation the Serilog:WriteTo:1:Args:token was already an Environmental variable?

In my case the storage account name and key were two separate environment variables which needed to be combined into a connections string at run time.

@skomis-mm
Copy link
Contributor

@SCC-BDuncan
Copy link

Anyone get a working example of this?

@PureKrome
Copy link
Author

@SCC-BDuncan

it's something like this:

given the configuration file snippet:

 "Serilog": {
    "WriteTo": [
      {
        "Name": "Console"
      },
      {
        "Name": "Logentries",
        "Args": {
          "token": "SOME SECRET HERE",
          "outputTemplate": "{Level:u3}: {Message:lj}{NewLine}{Exception}"
        }
      }
    ]  
  }

in your env-vars (like Azure App Service) you would have

Key Value Notes
Serilog__WriteTo__1__Args__token Some Secret Here Works on all OS's
Serilog:WriteTo:1:Args:token Some Secret Here Might not work on some OS's because the : is reservered

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants