【发布时间】: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