带有附加项目的NLog配置

时间:2012-11-27 17:03:36

标签: c# .net app-config nlog

在我的解决方案中,有一个使用NLog的主项目,还有另一个小型(独立)项目,其目的是允许更新解决方案配置文件中<nlog>部分的属性。 (SolutionName.exe.config)

我创建了继承自ConfigurationElement,ConfigurationElementCollection和ConfigurationSection的类,以便能够读取和写入配置文件。

我必须在配置文件中定义NLog部分,否则NLog将无法工作。 我的配置文件如下所示:

<configSections>
  <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" /> <- need to remove this to make it work
  <section name="nlog" type="ReadWriteConfig.NLogSection, ReadWriteConfig" />
</configSections>

我不允许ReadWriteConfig引用NLog,它是一个独立的应用程序。

如果我以这种方式运行ReadWriteConfig,它会抛出一个异常,它无法找到NLog程序集。如果我删除了NLog <section>,它可以正常工作。

ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = @"somepath\SolutionName.exe.config";

// Get current configuration file.
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

NLogSection myUrlsSection = config.GetSection("nlog") as NLogSection;

处理配置文件的正确方法是什么?

0 个答案:

没有答案