使用 Nlog 和数据库目标,有没有办法在运行时设置连接信息?
最佳答案
我记得我的一些同事做过类似的事情。他找到了这个答案:http://nlog-forum.1685105.n2.nabble.com/DDL-for-Database-Table-How-to-set-Connection-String-Programmatically-td5241103.html一切正常。
来自上面的链接
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();
关于c# - Nlog Target Database 在运行时设置连接信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11891329/