c# - Nlog Target Database 在运行时设置连接信息

标签 c# nlog

使用 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/

相关文章:

c# - 如何设置 NLog 最大文件大小?

c# - Cookie时间问题

c# - 我可以保存 ObservableCollection 的内容,以便在我重新启动我的应用程序时列表仍然完好无损吗?

zip - 压缩旋转日志文件的实用程序?

编程配置中的 c# NLog 自定义定界符

nlog - 当记录到数据库失败时如何强制nlog抛出异常?

c# - XmlSerializer.Serialize 不明确

c# - 链接到 .Net 安装项目中的配置文件

java - 重构具有大量级别之间依赖关系的内部循环

c# - nlog 不创建文件