2015-11-13 91 views
0

我们正在使用Enterprise Library 5.0并希望使用日志记录应用程序块将日志/跟踪消息写入数据库。执行web.config文件中的所有配置就像冠军一样工作。企业库5混合配置

但是,当应用程序启动时,我们需要能够指定记录器动态使用的数据库连接字符串。我们认为我们可以通过使用Ent Lib 5.0 Fluent API以编程方式注册连接字符串,然后将其与web.config中的配置值合并,从而采用混合方法。这将允许我们能够重新配置跟踪消息的写入方式/位置,而无需更改代码。

这是我们曾尝试:如果我检查生成器对象在

<loggingConfiguration ...> 
    <listeners> 
     <add databaseInstanceName="TestDB" ... /> 
    </listeners> 
    <formatters>...</formatters> 
    <categorySources>...</categorySources> 
    <specialSources>...</specialSources> 
</loggingConfiguration> 

var dynamicConnectionString = "..." // value determined elsewhere 

var builder = new ConfigurationSourceBuilder(); 
builder.ConfigureData() 
    .ForDatabaseNamed("TestDB").ThatIs.ASqlDatabase() 
    .WithConnectionString(dynamicConnectionString); 

var configSource = new SystemConfigurationSource(); 
// the line below throws an ArgumentException with the message 
// 'Cannot add a ConfigurationSection with the same name that already exists' 
builder.UpdateConfigurationWithReplace(configSource); 
EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource); 

web.config文件我们的测试项目,我们只有如下定义loggingConfiguration节调试器,它只定义了1个部分:一个connectionStrings部分,其中包含一个带有通过代码指定的值的单个connectionString条目。 web.config文件不包含包含connectionStrings部分。具有讽刺意味的是,如果我使用“立即/观察窗口”并检查System.Configuration.ConfigurationManager.ConnectionStrings,它会报告已经定义了1个连接...默认连接("data source=.\SQLExpress;Integrated Security=SSPI;AttacheDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true")。

如果我添加<connectionStrings><clear /></connectionStrings>清除继承的值,当我再次调试我仍然得到同样的异常,因为之前和 VS通知我,web.config文件已被更新(如果我重新加载该文件,该<connectionStrings>部分有已被删除)。

我在这里错过了什么!这不可能是那么难!

回答

1

在周末考虑了这个问题并做了更多的挖掘之后,我发现了一个帮助我解决问题的Blog Post。这是它如何为我工作:

var builder = new ConfigurationSourceBuilder(); 
// Configure the dataSource (connection string) 
builder.ConfigureData() 
    .ForDatabaseNamed("TestDB") 
     .ThatIs.ASqlDatabase() 
     // this would be determined at runtime; not statically defined 
     .WithConnectionString(@"Data Source=.\SQLExpress;Initial Catalog=Logging;Integrated Security=True") 
    .AsDefault(); 

// create a new config source 
var configSource = new DictionaryConfigurationSource(); 
builder.UpdateConfigurationWithReplace(configSource); 

IUnityContainer container = new UnityContainer(); 

// load the default configuration information from the config file 
// i.e. ConfigurationSourceFactory.Create() 
// and add it to the container 
container.AddNewExtension<EnterpriseLibraryCoreExtension>(); 

// create a configurator to use to configure our fluent configuration 
var configurator = new UnityContainerConfigurator(container); 
EnterpriseLibraryContainer.ConfigureContainer(configurator, configSource); 

// Use the configured container with file and fluent config as the Enterprise Library service locator 
EnterpriseLibraryContainer.Current = new UnityServiceLocator(container); 

// Write a log entry using the Logger facade 
LogEntry logEntry = new LogEntry 
{ 
    EventId = 180, 
    Priority = 1, 
    Message = "Message", 
    Categories = { "AccessAudit" } 
}; 

Logger.Write(logEntry); 

希望这可以帮助别人在未来。

+0

很高兴你发现这个博客很有用。 –