如何在C#代码中设置Database Trace Listener中的databaseInstanceName?

时间:2013-07-30 11:30:17

标签: c# exception-handling enterprise-library tracelistener

这可能是一个简单的问题。有没有什么办法可以在数据库跟踪侦听器中以编程方式设置databaseInstanceName?当我的应用程序与不同的数据库交互时(比如在connectionString标签中我有3个指向不同数据库的连接字符串),所以我需要在db中记录异常,但是要进入相应的数据库。我没有想要为日志记录创建一个单独的数据库。

有办法吗?

1 个答案:

答案 0 :(得分:0)

使用fluent API配置Enterprise Library ...

var configurationSourceBuilder = new ConfigurationSourceBuilder();

// do other configuration here

configurationSourceBuilder
    .ConfigureLogging()
        .LogToCategoryNamed("Category")
            .SendTo.Database("Database Trace Listener")
                .UseDatabase("DatabaseInstance");

// or here

var configurationSource = new DictionaryConfigurationSource();
configurationSourceBuilder.UpdateConfigurationWithReplace(configurationSource);
EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configurationSource);

如果您有三个连接字符串的原因是您的程序必须支持在三个不同的环境(dev / test / production?)中运行,那么更好的选择可能是改为使用单个连接字符串。然后部署为每个环境定制的配置文件,以便所有环境的代码都相同。