Search code examples
.netapp-confignlogilmerge

Can't read custom config section after using ILMerge


Subject: I create console application (documents parser) and use NLog for logging processing events. After build i use ILMerge tool for merging all DLLs to one executable file (it needed for more confortable using result tool)

If i use external NLog.config xml-file for loggers configuration - it's work good (before and after merging all dll-s and exe to single file)

So, problem: But if i trying to use "nlog" section in docsloader.exe.config file - it's work only without merging by IL (if all dll and exe files in separately mode). If i firstly merge result to single file and run docsloader.exe i see:

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 (first 20 strings)

<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 command:

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: If i remove -section and create NLog.config - it's work correctly. Thank you!


Solution

  • The error comes from the type reference NLog.Config.ConfigSectionHandler, NLog which refers to NLog dll (after the comma), which no longer exists as it's now merged.

    You can replace it by NLog.Config.ConfigSectionHandler, docsloader and it'll probably work.