问题
Using Nlog and a Database target, is there a way to set the connection information at runtime?
回答1:
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();
来源:https://stackoverflow.com/questions/11891329/nlog-target-database-set-connection-information-at-runtime