I have set up my solution and serilog reads the values from appsettings.json
public void Configure(IApplicationBuilder application, IHostingEnvironment environment, ILoggerFactory loggerFactory)
{
Logger log = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.CreateLogger();
loggerFactory.AddSerilog(log);
And the appsettings looks like this:
"Serilog": {
"Using": [ "Serilog.Sinks.Slack", "Serilog.Sinks.ApplicationInsights" ],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Error",
"System": "Error"
}
},
"WriteTo": [
{
"Name": "Slack",
"Args": {
"WebHookUrl": "https://hooks.slack.com/services/xxx/yyy/zzz",
"CustomChannel": "#channel"
}
},
{
"Name": "ApplicationInsights",
"Args": {
"InstrumentationKey": "xxx-xxx-xxx-xxx-xxx",
"restrictedToMinimumLevel": "Information",
"telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
}
}
]
}
So everything is fine til here, but I don't want to store secrets like webhook and InstrumentationKey in my repository. I want to read them from a safe place like user-secrets on development environment and Key-vault on prouction. How do I change it to replace the values from usersecrets?
https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-2.2&tabs=windows
Is there anyways the config get overriten automatically?
If not, I know I can read my configuration from secrets let's say like
webHookUrl = Configuration["Serilog:WriteTo:Slack:WebHookUrl"];
But then how do I apply it on the loggerConfig? I mean, the writeTo part is dynamic from the settings, should I then add it in code instead? :\
Update
Since many people have asked, in my program.cs I have:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseApplicationInsights()
.UseStartup<Startup>();
And based on the Microsoft documentation, everything should work because I have CreateDefaultBuilder
and I am running .net core 2.2.
https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-2.2&tabs=windows#access-a-secret
In ASP.NET Core 2.0 or later, the user secrets configuration source is automatically added in development mode when the project calls CreateDefaultBuilder to initialize a new instance of the host with preconfigured defaults. CreateDefaultBuilder calls AddUserSecrets when the EnvironmentName is Development.
When CreateDefaultBuilder isn't called, add the user secrets configuration source explicitly by calling AddUserSecrets in the Startup constructor. Call AddUserSecrets only when the app runs in the Development environment
builder.AddUserSecrets<Startup>();
? – ESG