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.