Log4net outputs information to RichTextBox

Source: Internet
Author: User
Tags log4net

I uploaded and checked log4net last night, because I was familiar with using this component in Java and have not extended it. However, a small project requires that log4net be rewritten so that log4net can output log messages to the desired place.
Let's talk about the use of log4net, and rewrite log4net to print the log messages to what we want.
1: Use of log4net:
A: Download incubating-log4net-1.2.10.zip, decompress it, find bin \ net \ 2.0 \ release \ log4net. dll (for. Net framework2.0 platforms), and introduce this DLL to your winform project.
B: 1) Create the app. config file under the Home Directory of your winform project. The content is as follows:

<?xml version="1.0" encoding="utf-8" ?><configuration>  <configSections>    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />  </configSections>  <log4net>    <root>      <level value="ALL" />      <appender-ref ref="LogFileAppender" />    </root>    <appender name="LogFileAppender" type="log4net.Appender.FileAppender" >      <layout type="log4net.Layout.PatternLayout">        <param name="ConversionPattern" value="%d %-5p [%c:%L]:%m%n" />      </layout>    </appender>  </log4net></configuration>

2) Add code at the bottom of assemblyinfo. CS under properties in your project:

// Add log4net log support
[Assembly: log4net. config. xmlconfigurator (configfileextension = "Config", watch = true)]

C: I added the following test code in the form onload event:
// Test for log4net
Ilog log = logmanager. getlogger (this. GetType ());
Log. Info ("test ");
D: run the program. You can find the log-file.txt file in the directory of the .exe program. The content is text.
Now, the simple use of log4net is OK.

2: Expand log4net so that logs can be output to what we want. In fact, log4net comes with a lot of appender output to different places, but the system does not meet my requirements here by default, I want the log to be displayed in RichTextBox. See my rewrite below:
A: Create your own log class and inherit from the appenderskeleton class of log4net. You only need to reload its append method. The code below is as follows:

Using system; using system. io; using system. collections. generic; using system. text; using log4net. appender; using log4net. filter; using log4net. util; using log4net. layout; using log4net. core; namespace postapplication. core. util {class log: appenderskeleton {override protected void append (loggingevent) {stringwriter writer = new stringwriter (); this. layout. format (writer, loggingevent); // you have obtained The log message content in the format set by yourself is writer. tostring (). Then you can display this sentence wherever it is .. The console is directly used for testing. Console. Write (writer. tostring ());}}}

 

B: Modify the app. <appender name = "logfileappender" type = "log4net. appender. fileappender "> <appender name =" logfileappender "type =" postapplication. core. util. log ">
..

After checking the information last night and testing today, we found that it is not that complicated ..

 

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.