Search code examples
c#nlog

NLog doesn't writes diacritics chars properly


I am trying to log a message to a file with nlog. My problem is, whenever I try to use polish chars as "ąćęśł" they show up as ¹æê³ in log file. Other countries special characters does work (like ä ö etc.). What may be the problem? This is how my beginning of nlog.config looks like:

  <?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          autoReload="true"
          internalLogLevel="Info">

        <targets>
            <target
                xsi:type="File"
                name="allInfo"
                fileName="${whenEmpty:whenEmpty=${basedir}:inner=${configsetting:item=PathToFiles}}/logs/${shortdate}.log"
                layout="${longdate} ${uppercase:${level}} ${message}"
                archiveEvery="Month"
                archiveFileName="${whenEmpty:whenEmpty=${basedir}:inner=${configsetting:item=PathToFiles}}/logs/archive/log-${shortdate}.log"
                maxArchiveFiles="50"/>
        </targets>

        <rules>
            <logger
                name="*"
                minlevel="Trace"
                writeTo="allInfo"/>
        </rules>
    </nlog>
</configuration>

As you can see utf-8 is there. No idea how to fix it.


Solution

  • Add encoding="utf-8" to the file target.

    encoding - File encoding name like "utf-8", "ascii" or "utf-16". See Encoding class on MSDN. Defaults to Encoding.Default.

    <target
        xsi:type="File"
        name="allInfo"
        ...
        encoding="utf-8"
    />