Search code examples
encodingutf-8configurationnlog

NLog not using UTF-8 encoding


Since I upgraded NLog to the latest version 5.1.0, logging special characters (e.g. Ö, ä or ß) stopped working. Outout to file is like: ö ä ü for these chars.

I checked the log file with NotePad++ and it says that the encoding is ANSI. However, I have configured NLog to use utf-8. My config:

  <target name="logfile" xsi:type="File" fileName="${basedir}/Logs/nLog.csv" archiveAboveSize="50000000" archiveNumbering="Sequence" maxArchiveFiles="3" encoding="utf-8" keepFileOpen="true">
      <layout xsi:type="CsvLayout">
        <column name="time" layout="${longdate}" />
        <column name="level" layout="${level}"/>
        <column name="category" layout="${event-context:item=category}" />
        <column name="message" layout="${message}" />
      </layout>
    </target>

What am I missing?


Solution

  • Based on the comments, I was able to solve the problem. However, I do not know what the real cause of the problem was.

    Solution: remove encoding="utf-8" from the nLog config AND read the log file as Encoding.UTF-8 (i.e. when opening the file with a StreamReader).

    I don't know why this works since nLog uses UTF-8 as default...