0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NLogでencoding="utf-8"が効かない

Last updated at Posted at 2018-11-15

訂正とお詫び

コメントでご指摘いただきましたがBOMがないことでエディターが、SJISとUTF-8を判別できないことによることが原因でした。
以下の記事は間違いでwriteBom=trueとしなくてもUTF-8で出力可能です。

環境

NLog 4.5.11

encodingの指定

以下のようにencoding="utf-8"とすればログファイルはutf-8になるはずだが、SJISで出力される。

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">
  <targets>
    <target name="event"
            xsi:type="File"
            encoding="utf-8"
            layout="${longdate} ${message}"
            fileName="${basedir}/logs/event.log" />
  </targets>
  <rules>
    <logger name="*" levels="Info,Warn" writeTo="event" />
  </rules>
</nlog>

対処

writeBom="true"を指定したらutf-8で出力されるようになりました。
writeBom="false"では変わらずSJISで出力されます。

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">
  <targets>
    <target name="event"
            xsi:type="File"
            encoding="utf-8"
            writeBom="true"
            layout="${longdate} ${message}"
            fileName="${basedir}/logs/event.log" />
  </targets>
  <rules>
    <logger name="*" levels="Info,Warn" writeTo="event" />
  </rules>
</nlog>
0
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?