Why log4net log message have some unknown character?

我的未来我决定 提交于 2021-01-27 20:46:07

问题


I use log4net for logging my logs. and use configuration here (only log the message):

    <logger name="LogTracking">
      <level value="INFO" />
      <appender-ref ref="LogTrackingAppender" />
    </logger>
    <appender name="LogTrackingAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logging\urltracking\" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <maxSizeRollBackups value="-1" />
      <maximumFileSize value="20MB" />
      <encoding value="utf-8" />
      <datePattern value="yyyy'\\'MM'\\'dd'\\'yyyyMMddHH'.log'" />
      <staticLogFileName value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message%newline" />
      </layout>
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    </appender>

But when i open my logs file, if in UTF-8 encoding the lines message seems normal. And if in ANSI encoding, these lines message have a unknown character at the beginning of the lines. See here:

<?xml version="1.0" encoding="utf-8"?><UrlTrackingObj><Url>mysite</Url><Action>view</Action><Ip>::1</Ip><Os>Windows 8.1</Os><Browser>Chrome 59.0</Browser></UrlTrackingObj>

Then when my app reads log lines, it can read and process this character. What is my configuration wrong? or any solution this solve this problem? I want my log line only:

<?xml version="1.0" encoding="utf-8"?><UrlTrackingObj><Url>mysite</Url><Action>view</Action><Ip>::1</Ip><Os>Windows 8.1</Os><Browser>Chrome 59.0</Browser></UrlTrackingObj>

回答1:


Those characters  are the byte order mark (BOM):

The UTF-8 representation of the BOM is the (hexadecimal) byte sequence 0xEF,0xBB,0xBF. A text editor or web browser misinterpreting the text as ISO-8859-1 or CP1252 will display the characters  for this.

You can use the other encoding class rather than the default one to suppress the BOM such as:

<encoding type="System.Text.UTF8Encoding"/>

System.Text.UTF8Encoding



来源:https://stackoverflow.com/questions/44838062/why-log4net-log-message-have-some-unknown-character

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!