32

I would like to log data to a file in 2 byte languages (chinese, japanese etc) using log4net.

How to properly configure log4net to do that?

nakhli
  • 4,009
  • 5
  • 38
  • 61
  • 1
    [The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets](http://www.joelonsoftware.com/articles/Unicode.html) - Unicode doesn't always mean 2 bytes. – Justin Aug 05 '11 at 12:27
  • @Kragen agree. I'm interested in the 2 bytes case. See examples I gave. – nakhli Aug 05 '11 at 12:35
  • So you mean UTF-16? (UTF-8 is more common nowadays) – Justin Aug 05 '11 at 12:36
  • 1
    @Kragen How to configure log4net to handle any encoding I want? – nakhli Aug 05 '11 at 13:10

1 Answers1

76

The log file encoding is specified by FileAppender.Encoding. It can be configured using the encoding configuration element. Example:

<appender name="FileAppender" type="log4net.Appender.FileAppender">
    <file value="file.log" />
    <encoding value="utf-8" />
    ...

The value is the code page name. The corresponding Encoding is obtained using the System.Text.Encoding.GetEncoding(string) method. For a list of code pages, see the Encoding class documentation.

Qny
  • 83
  • 2
  • 5
nakhli
  • 4,009
  • 5
  • 38
  • 61