Unable to set my connectionstring in NLog
Updated answer
Since NLog.Web.AspNetCore 4.8 (NLog.Extensions.Logging 1.4 for .NET Core console programs) you could directly read from your appSettings.json
${configsetting:name=MyConnectionString}
see docs
Original answer
Unfortunately reading connectionstrings/settings from appSettings.json / app.config is not yet supported in NLog for .NET core.
Two options:
Set the connectionstring programmatically, by using variables. In your nlog.config:
<target ... connectionString="${var:myConnectionstring}" ... />
and in code: (e.g. in
Configure
)LogManager.Configuration.Variables["myConnectionstring"] = "...."; //read config here
Or, set the connectionstring in nlog.config.
In your nlog.config:
<variable name="myConnectionstring" value="...." />
and using in your target in nlog.config:
<target ... connectionString="${var:myConnectionstring}" ... />
Another option is to create and register a custom NLog layout-renderer (startup.cs):
https://github.com/NLog/NLog/wiki/How-to-write-a-custom-layout-renderer
Which outputs the ConnectionString after having read it from your favorite configuration-location. Then you don't have the connectionstring in your nlog.config, but just refer to your custom layout-renderer.
Maybe cheer for this pending issue:
https://github.com/NLog/NLog.Web/issues/107