使用 ILMerge 后无法读取自定义配置部分

Ale*_*lov 3 .net ilmerge app-config nlog

主题:我创建控制台应用程序(文档解析器)并使用 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.
Run Code Online (Sandbox Code Playgroud)

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>

....
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

Glu*_*uck 5

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

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