如何从字符串加载企业库配置

时间:2010-11-11 20:49:40

标签: c# .net enterprise-library

我将Enterprise Library配置XML序列化并加载到String对象中。

有没有办法用该字符串初始化Enterprise Library日志记录组件?

我正在寻找类似的东西:

IConfigurationSource configSource = new SomethingConfigurationSource(stringWhichHasConfig);
LogWriterFactory logFactory = new LogWriterFactory(configSource);
LogWriter writer = logFactory.Create();

有没有办法真正做到这一点?

2 个答案:

答案 0 :(得分:0)

将字符串反序列化为ent lib理解的配置对象。

答案 1 :(得分:0)

我的解决方法是将配置写入临时文件,并按如下方式加载该配置:

string loggingConfigSetting = GetSetting("LoggingConfiguration");

string tempConfigPath = Path.GetTempFileName();
File.AppendAllText(tempConfigPath, loggingConfigSetting);

FileConfigurationSource configSource = new FileConfigurationSource(tempConfigPath);
LogWriterFactory logFactory = new LogWriterFactory(configSource);
writer = logFactory.Create();