[. Net] Tab character for log4net message output (Tab character)

Source: Internet
Author: User
Tags log4net

 

Preface:

Log4net is a third-party software that is convenient for log processing.
However, the "partially supported" Tab character is output in the output part ).
The so-called "partially supported" means,
It is supported in the message content, but not in the message layout.
This imposes a lot of restrictions on developers when composing messages.

 

This article records,
When using log4net to output log messages, you can use the Tab character ).
Hope to help developers in need.

 

Practice:

The actual method is to use the log4net custom Parameter Function,
Add a custom Tab character to generate a Tab character ).
Steps:

 

1. Use the custom parameter tab in the log4net configuration file.
Format: % x {tab}

<?xml version="1.0"?><log4net>       <!-- Logger -->  <logger name="Default">    <level value="ALL" />    <appender-ref ref="Default" />  </logger>      <!-- Appender -->  <appender name="Default" type="log4net.Appender.RollingFileAppender">    <file value="ConsoleApplication1.log" />    <appendToFile value="true" />        <immediateFlush value="true" />    <maximumFileSize value="100K"/>    <maxSizeRollBackups value="1"/>        <layout type="log4net.Layout.PatternLayout">      <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.fff} %m %X{tab} ----- %n" />    </layout>    <staticLogFileName value="true"/>    <rollingStyle value="Size"/>  </appender>      </log4net>

 

2. Add the custom parameter tab to log4net in the program code.
Adding method: log4net. MDC. Set ("tab", "\ t ");

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace consoleapplication1 {class program {static void main (string [] ARGs) {// initialize log4net log4net. config. xmlconfigurator. configure (new system. io. fileinfo ("log4net. XML "); // Add the custom parameter tab log4net. MDC. set ("tab", "\ t"); // write log log4net. logmanager. getlogger ("default "). error ("sample message1"); log4net. logmanager. getlogger ("default "). error ("sample message12"); log4net. logmanager. getlogger ("default "). error ("sample message123"); // end log4net log4net. logmanager. shutdown ();}}}

 

After completing the preceding steps,
Output log messages according to the general usage of log4net.
View the output log file and find that the Tab character output is supported correctly.

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.