Search code examples
.netwindowsunicodelog4netlog4net-configuration

log4net works well in window 10 but only wirte part of logs in windows 7 and xp


I use log4net to log info and error in my application, it works well in windows 10, but in windows 7 and windows xp, it only write part of the logs, looks like below

enter image description here

This is my log4net config, anyone can help?

<log4net>
  <logger name="logerror">
    <level value="ALL" />
    <appender-ref ref="ErrorAppender" />
  </logger>
  <logger name="loginfo">
    <level value="ALL" />
    <appender-ref ref="InfoAppender" />
  </logger>
  <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value="Log\\LogError\\" />
    <param name="AppendToFile" value="true" />
    <param name="MaxSizeRollBackups" value="100" />
    <param name="MaxFileSize" value="10240" />
    <param name="StaticLogFileName" value="false" />
    <param name="DatePattern" value="yyyyMMdd&quot;.htm&quot;" />
    <param name="RollingStyle" value="Date" />
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="&lt;HR COLOR=red&gt;%n异常时间:%d [%t] &lt;BR&gt;%n异常级别:%-5p &lt;BR&gt;%n异 常 类:%c [%x] &lt;BR&gt;%n%m &lt;BR&gt;%n &lt;HR Size=1&gt;"  />
    </layout>
  </appender>
  <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value="Log\\LogInfo\\" />
    <param name="AppendToFile" value="true" />
    <param name="MaxFileSize" value="10240" />
    <param name="MaxSizeRollBackups" value="100" />
    <param name="StaticLogFileName" value="false" />
    <param name="DatePattern" value="yyyyMMdd&quot;.htm&quot;" />
    <param name="RollingStyle" value="Date" />
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="&lt;HR COLOR=blue&gt;%n日志时间:%d [%t] &lt;BR&gt;%n日志级别:%-5p &lt;BR&gt;%n日 志 类:%c [%x] &lt;BR&gt;%n%m &lt;BR&gt;%n &lt;HR Size=1&gt;"  />
    </layout>
  </appender>
</log4net>

Solution

  • I suspect that you refer to the ? characters. ? and are replacement characters that are used when an invalid codepage conversion is attempted, eg from Unicode to a codepage that can't handle all the characters. For example, trying to write Japanese or Chinese characters to a file using the Latin-1 codepage will replace all non-Latin characters with ? or .

    This seems to be the problem here. By default, log4net uses the Default encoding, which correspond's to system's locale, to write to files.

    You can specify the encoding explicitly with the FileAppender.Encoding property:

    <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
        <encoding value = "utf-8" />
    
    <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
        <encoding value = "utf-8" />