Nlog Target Database set connection information at runtime

做~自己de王妃 提交于 2019-12-10 17:10:10

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!