为什么log4net日志消息中有一些未知字符? [英] Why log4net log message have some unknown character?

查看:63
本文介绍了为什么log4net日志消息中有一些未知字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用log4net记录我的日志.并在此处使用配置(仅记录消息):

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>

但是当我打开日志文件时,如果以UTF-8编码,则行消息似乎是正常的.并且,如果采用ANSI编码,则这些行消息在行的开头具有未知字符.看到这里:

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>

推荐答案

那些字符字节顺序标记(BOM):

BOM的UTF-8表示形式是(十六进制)字节序列0xEF,0xBB,0xBF.文本编辑器或Web浏览器误解了文本因为ISO-8859-1或CP1252会为此显示字符»".

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.

您可以使用其他编码类而不是默认的编码类来抑制 BOM ,例如:

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

这篇关于为什么log4net日志消息中有一些未知字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆