0

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>
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Allen4Tech
  • 2,094
  • 3
  • 26
  • 66
  • log4net is an old library. Obviously, it works in Windows 7 and even unsupported ancient OSs like Windows XP. What is the screenshot supposed to show? What is the *actual* problem and how do you write to the log? What do you mean `it only write part of the logs` ? – Panagiotis Kanavos Jul 17 '17 at 08:09
  • Are you referring to the question marks? `?` or `�` are used when an invalid codepage conversion is attempted. This could mean that you tried to convert ASCII text to Unicode using an invalid codepage. **App.config itself** may be stored as ASCII instead of UTF8, in which case the conversion pattern itself will result in bad conversions. If you intend to store non-ASCII text in `app.config` you have to ensure it's saved as UTF8 – Panagiotis Kanavos Jul 17 '17 at 08:13
  • Possible duplicate of [Log4Net: Logging in 2 byte languages (japanese, chinese etc.)](https://stackoverflow.com/questions/6953424/log4net-logging-in-2-byte-languages-japanese-chinese-etc) – Panagiotis Kanavos Jul 17 '17 at 08:26
  • 1
    Post the *actual* output. The conversion pattern contains HTML tags. I fhte message started with ` – Panagiotis Kanavos Jul 17 '17 at 09:24

1 Answers1

2

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" />
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • Hi @Panagiotis, thanks for your reply. it doesn't related to encoding, even change the encoding, the content still doesn't been write. You can see only '[' display after 'loginfo', it should contain other content after '['. For example, [Login successfully]. – Allen4Tech Jul 17 '17 at 09:17
  • @Allen4Tech explain what's wrong in the question itself and include actual code that rerproduces the problem . One can only guess what you mean. The question marks are *definitely* related to encoding. – Panagiotis Kanavos Jul 17 '17 at 09:21
  • @Allen4Tech and post the *actual* output, not the screenshot. Your pattern contains HTML characters. The output values may result in invalid HTML. Imagine what would happen eg if the message started with ` – Panagiotis Kanavos Jul 17 '17 at 09:22