Problems in Chinese when log4net and log2console are used together

Source: Internet
Author: User

Log2console is a visual log viewing tool. Chinese characters are garbled during use.

The solution is as follows.

I,Log4net cannot output Chinese logs

Cause: log4net. appender. udpappender of log4net must be used in combination with log2console to output logs and shield all non-Chinese characters. Log4net source code problem occurs in the util \ transform. CS file public static string maskxmlinvalidcharacters (string textdata, string mask)
This function is intended to replace the invalid characters in the output message "? ". Unfortunately, this regular expression is incorrect.
Private Static RegEx invalidchars = new RegEx (@ "[^ \ x09 \ x0a \ x0d \ x20-\ xFF \ u00ff-\ u07ff \ ue000-\ ufffd]", regexoptions. compiled );
This regular expression replaces a large part of the characters, including Chinese characters and Japanese and Korean characters.
Solution: I commented out the regular expression replacement sentence and re-compiled it.

Change return invalidchars. Replace (textdata, mask); to return textdata;

II,Log2console cannot display Chinese logs
1) Use log2console to monitor system file logs. cause:Log2consoleThe source code of is not specified when _ filereader is created
Solution: Modify the initialize method of the filereceiver. CS file and SET _ filereader = new streamreader (New filestream (_ filetowatch, filemode. Open, fileaccess. Read, fileshare. readwrite ));
Changed:
_ Filereader = new streamreader (New filestream (_ filetowatch, filemode. Open, fileaccess. Read, fileshare. readwrite), system. Text. encoding. Default );
Then re-compile.

2) use the UDP method of log2console to obtain the log cause:Log2consoleTo obtain UDP information, use UTF-8 to obtain the string.

Solution: Modify the START () method of the udpreceiver. CS file

String loggingevent = system. Text. encoding. utf8.getstring (buffer );

Changed:

String loggingevent = system. Text. encoding. Default. getstring (buffer );

Then re-compile.

Log2consoleSource code can be downloaded at the address below

Http://log2console.codeplex.com/

 

After compilationLog4net.rarAndLog2console

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.