Using Nlog and a Database target, is there a way to set the connection information at runtime?
I remember some colleague of mine doing something similar. He found this answer: http://nlog-forum.1685105.n2.nabble.com/DDL-for-Database-Table-How-to-set-Connection-String-Programmatically-td5241103.html that was working perfectly fine.
From the link above
The easiest way of overriding connection string is through the use of GDC:
<target name="db" type="Database" connectionString="${gdc:myConnectionstring}" ... />
Now in your code you can simply do:
GDC.Set("myConnectionString", "Server=.;database=.....");
You can also modify the target:
var config = LogManager.Configuration;
var dbTarget = (DatabaseTarget)config.FindTargetByName("db");
dbTarget.ConnectionString = "server=.;...";
LogManager.ReconfigExistingLoggers();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With