Closed igorgomeslima closed 1 year ago
This is provided natively by Microsoft.Extensions.Configuration; I think the variable you'd need to set in this case is something like SERILOG__WRITETO__1__ARGS__APIKEY
.
Might need a bit of digging around, unfortunately setting values in arrays using environment variables can be awkward. HTH!
@nblumhardt Thanks for your time! Any specific documentation about it? I thought of using: Placeholder Provider instead.
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 š
@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.
Thank's for insights guys! I know about the "Configuration", when I mentioned "Documentation" I meant specifically to "How read from appsettings using index
? How index works in appsettings? Any official sample at DOC's, like Serilog__WriteTo__1__Args__apiKey
or similar?". Anyway, it's help. š
It's all in the Configuration in ASP.NET Core documentation. Look at Bind an array and Naming of environment variables specifically.
Serilog Settings Configuration(Serilog.Sinks.Datadog.Logs):
Sample:
We have something similar in NLog:
${environment:ENVIRONMENT_VARIABLE_NAME}
.Environment layout renderer
Sample:
NLog.config(NLog.Targets.ElasticSearch)