我们正在使用Enterprise 5.0,并且希望使用日志应用程序块向数据库写入日志/跟踪消息。执行web.config文件中的所有配置就像一个champ。
但是,当应用程序启动时,我们需要能够指定记录器动态使用的数据库连接字符串。我们认为,通过使用EntLib5.0FLUENT API以编程方式注册连接字符串,然后将其与来自web.config的配置值合并,我们可以采取一种混合方法。这将使我们能够重新配置跟踪消息的编写方式/位置,而不必更改代码。
这就是我们尝试过的:
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部分如下:
<loggingConfiguration ...>
<listeners>
<add databaseInstanceName="TestDB" ... />
</listeners>
<formatters>...</formatters>
<categorySources>...</categorySources>
<specialSources>...</specialSources>
</loggingConfiguration>如果我检查调试器中的构建器对象,它只定义了一个部分:一个connectionStrings部分,其中包含一个connectionString条目,其中包含我通过代码指定的值。web.config文件根本不包含connectionStrings部分。
具有讽刺意味的是,如果我使用即时/监视窗口并检查System.Configuration.ConfigurationManager.ConnectionStrings,它会报告已经定义了一个连接.默认("data source=.\SQLExpress;Integrated Security=SSPI;AttacheDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true")连接。
如果我添加<connectionStrings><clear /></connectionStrings>以清除继承的值,当我再次调试时,仍然会得到和 VS通知我web.config文件已经更新之前的异常(如果我重新加载该文件,那么<connectionStrings>部分已经被删除)。
我在这里错过了什么!这不会那么难的!
发布于 2015-11-16 19:42:00
在周末考虑了这个问题并做了进一步的调查之后,我发现了一个博客邮报,它帮助我解决了我的问题。我就是这样做的:
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 );希望这对将来的其他人有帮助。
https://stackoverflow.com/questions/33702136
复制相似问题