question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

It's possible to read "credentials/sensitive info" from environment variables?

See original GitHub issue

Serilog Settings Configuration(Serilog.Sinks.Datadog.Logs):

Sample:

"Serilog": {
  "Using": [ "Serilog.Sinks.Datadog.Logs" ],
  "MinimumLevel": "Debug",
  "WriteTo": [
    { "Name": "Console" },
    {
      "Name": "DatadogLogs",
      "Args": {
        //"apiKey": "<API_KEY>",
        "apiKey": "${READ_IT_FROM_ENVIRONMENT_VARIABLE:VARIABLE_NAME}", //<- This approach does not exist/does not work.
      }
    }
  ],
  "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
  "Properties": {
    "Application": "Sample"
  }
}

We have something similar in NLog: ${environment:ENVIRONMENT_VARIABLE_NAME}.

Environment layout renderer

Sample:

NLog.config(NLog.Targets.ElasticSearch)

...
<target xsi:type="ElasticSearch"
                ...
                username="${environment:ELASTICSEARCH_USER}"
                password="${environment:ELASTICSEARCH_PASSWORD}"
                layout="${verbose}">
          <field name="host" layout="${machinename}"/>
        </target>
...

Issue Analytics

  • State:closed
  • Created 8 months ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
0xcedcommented, Feb 6, 2023

@nblumhardt’s answer is right, you need to set the Serilog__WriteTo__1__Args__apiKey environment variable in order to override the value from the appsettings.json file.

The __1__ is the (zero-based) index in the WriteTo array, it’s indeed awkward and error prone if you reorder your sinks in the configuration file.

Official documentation:

Sample.csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
    <PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
    <PackageReference Include="Serilog.Sinks.Datadog.Logs" Version="0.5.1" />
  </ItemGroup>

  <ItemGroup>
    <None Update="appsettings.json" CopyToOutputDirectory="PreserveNewest" />
  </ItemGroup>

</Project>

Program.cs:

using Microsoft.Extensions.Configuration;
using Serilog;

var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").AddEnvironmentVariables().Build();
using var logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).CreateLogger();
logger.Information("Hello, World!");

Run this sample code with the Serilog__WriteTo__1__Args__apiKey environment variable set to your API key and you should see your log in Datadog.

1reaction
nblumhardtcommented, Jan 30, 2023

It’s not a Serilog feature so docs.microsoft.com (IIRC) would be the place to find it but I wasn’t able to track anything down with a quick skim. Placeholder Provider looks like another good option šŸ‘

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it secure to store passwords as environment variables ...
There's no way to store an un-encrypted password securely. Now which of environment variables vs. config files is more "secure" is perhapsĀ ...
Read more >
Keep your code secure by using environment variables ...
Keep your credentials safe. The first reason is the most important by far: environment variables keep your credentials save. We want to preventĀ ......
Read more >
Is it unsafe to use environmental variables for secret data?
In general storing secrets in environment variables does have some downsides, as Diogo says in his post. Generically, for platforms likeĀ ...
Read more >
Analyzing the Hidden Danger of Environment Variables ...
This is because this practice is easy to implement and it could be dangerous if any confidential information is stored inside, leaked, and/orĀ ......
Read more >
Handling Passwords and Secret Keys using Environment ...
The safest way to handle your secret keys/password is saving them in envirnoment variables. In this post we will learn how to save...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found