【问题标题】:NLog doesn't writes diacritics chars properly [duplicate]NLog 不能正确写入变音符号 [重复]
【发布时间】:2021-11-14 03:25:18
【问题描述】:

我正在尝试使用 nlog 将消息记录到文件中。我的问题是,每当我尝试将波兰字符用作“ąćęśł”时,它们在日志文件中显示为 ¹æê³。其他国家的特殊字符确实有效(如 ä ö 等)。可能是什么问题? 这就是我的 nlog.config 开头的样子:

  <?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>

正如您所见,utf-8 就在那里。不知道如何解决。

【问题讨论】:

  • 那是配置文件的编码,与配置nlog本身无关。请也显示文件的其余部分。
  • @AKX 编辑后的帖子
  • 编码是UTF8 不是utf-8

标签: c# nlog


【解决方案1】:

添加encoding="utf-8"to the file target

encoding - 文件编码名称,如“utf-8”、“ascii”或“utf-16”。请参阅 MSDN 上的编码类。默认为 Encoding.Default。

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

【讨论】:

    猜你喜欢
    • 2013-05-18
    • 2017-08-13
    • 2014-01-04
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-11
    相关资源
    最近更新 更多