Log4net output information to RichTextBox

Source: Internet
Author: User
Tags log4net

Last night to upload a look at the next log4net, because before doing Java for this component of the use of more familiar, has not been extended. But a small project needs, to achieve the log4net rewrite, let log4net output log messages to the place they want to output.
First of all, the use of log4net, and then to Log4net rewrite, to enable log messages to print to the place we want.
Use of 1:log4net:
A: Download Incubating-log4net-1.2.10.zip, unzip, find Bin\net\2.0\release\log4net.dll (for. NET framework2.0 platform), Get your WinForm project into this DLL.
b:1) in your WinForm Engineering home directory to establish a file app.config, which reads 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 in your project under the properties of the AssemblyInfo.cs the following line to add code:

Add Log4net Log Support
[Assembly:log4net. Config.xmlconfigurator (configfileextension = "config", Watch = True)]

C: I added the following test code to the OnLoad event of form:
Test for Log4net
ILog log = Logmanager.getlogger (this. GetType ());
Log. Info ("test");
D: Run the program, you can find the file Log-file.txt in the. exe program's sibling directory, and the content is text.
So far, the simple use of log4net has been OK.

2: Expand Log4net, let the log output to the place we want, in fact, log4net with a lot of output to different places of appender, but I need here, the system or default does not meet me, I want to let the log displayed in the RichTextBox, the following see my rewrite:
A: Create your own log class, Inherit Log4net Appenderskeleton class, only need to overload its Append method, the following look at the code:

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 loggingevent)
        {
            StringWriter writer = new StringWriter ();
            This. Layout.format (writer, loggingevent);
     You've got the content of the log message in the format you set up, which is writer.tostring (). And then you want to show it anywhere. I was testing on the direct console.
            Console.Write (writer. ToString ());}}}



B: Modify App.config's <appender name= "Logfileappender" type= "log4net". Appender.fileappender "> For <appender name=" Logfileappender "type=" PostApplication.core.util.Log ">
Can..

After last night's review and today's test, it's 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.