Using NLog in asp.net core 2.0 web application
There is a wiki document about this:
https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2
To inject custom data like your connection-string, then just create and register a custom layout renderer:
https://github.com/NLog/NLog/wiki/How-to-write-a-custom-layout-renderer
Or put the connection-string into the NLog-Global-Diagnostic-Context at startup:
https://github.com/NLog/NLog/wiki/Var-Layout-Renderer
Maybe something like this where NLog.config
makes use of ${gdc:connectionString}
:
var myConnectionString = Configuration.GetConnectionString("myDb");
NLog.GlobalDiagnosticsContext.Set("connectionString", myConnectionString);
var logFactory = NLogBuilder.ConfigureNLog("NLog.config"); // Uses ${gdc:connectionString}
var logger = logFactory.GetCurrentClassLogger();
logger.Info("Hello World");
See also https://github.com/NLog/NLog/wiki/Gdc-Layout-Renderer
Update - ${configsetting}
NLog.Extension.Logging ver. 1.4 now supports ${configsetting}
so NLog can read settings from appsettings.json directly without needing to use NLog variables. See https://github.com/NLog/NLog/wiki/ConfigSetting-Layout-Renderer