【问题标题】:Can't read custom config section after using ILMerge使用 ILMerge 后无法读取自定义配置部分
【发布时间】:2026-01-06 04:55:01
【问题描述】:

主题: 我创建控制台应用程序(文档解析器)并使用 NLog 记录处理事件。 构建后,我使用 ILMerge 工具将所有 DLL 合并到一个可执行文件(它需要使用结果工具更舒适)

如果我使用外部 NLog.config xml 文件进行记录器配置 - 它工作得很好(在将所有 dll-s 和 exe 合并到单个文件之前和之后)

所以,问题: 但是,如果我尝试在 docsloader.exe.config 文件中使用“nlog”部分 - 它只能在不通过 IL 合并的情况下工作(如果所有 dll 和 exe 文件都处于单独模式)。 如果我首先将结果合并到单个文件并运行 docsloader.exe 我会看到:

Unhandled Exception: System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for nlog: Could not load file or
 assembly 'NLog' or one of its dependencies. The system cannot find the file specified. (C:\d\projects\_\out\docsloader.exe.Config line 5)
---> System.IO.FileNotFoundException: Could not load file or assembly 'NLog' or one of its dependencies. The system cannot find the file specified.

docsloader.exe.config(前 20 个字符串)

<configSections>
      <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
</configSections>  

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd">
  <targets>
        <target name="console" type="Console" layout="${date:format=HH\:mm\:ss}|${level}| ${message}" />
        <target name="file"  type="File" fileName="${basedir}/docsloader.log" layout="${date}|${level}| ${message}" />
    </targets>
    <rules>
        <logger name="*" minlevel="Debug" writeTo="console" />
        <logger name="*" minlevel="Trace" writeTo="file" />
    </rules>
  </nlog>

<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
</startup>

....

ILMerge 命令:

C:\d\projects\_\ILMerge\ILMerge.exe /out:"C:\d\projects\_\_\out\docsloader.exe" "C:\d\projects\_\_\bin\Release\docsloader.exe" "C:\d\projects\_\_\bin\Release\*.dll" /target:exe /targetplatform:v4,C:\Windows\Microsoft.NET\Framework64\v4.0.30319 /lib:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0" /wildcards

echo Coping docsloader.exe.config...
cp C:\d\projects\_\_\bin\Release\docsloader.exe.config C:\d\projects\_\_\out\docsloader.exe.config

PS:如果我删除 -section 并创建 NLog.config - 它可以正常工作。 谢谢!

【问题讨论】:

    标签: .net app-config nlog ilmerge


    【解决方案1】:

    错误来自类型引用NLog.Config.ConfigSectionHandler, NLog,它引用了NLog dll(在逗号之后),它现在已合并,因此不再存在。

    您可以将其替换为NLog.Config.ConfigSectionHandler, docsloader,它可能会起作用。

    【讨论】:

    • 太好了,谢谢! PS:我的错误是,我尝试在逗号后使用项目名称(而不是 NLog),但我不正确,因为 VS 中的构建脚本为输出 .exe 文件(ontoloader)设置了另一个名称。你的答案是可行的。
    最近更新 更多