log4net:将log实时显示在textbox中(WinForm)

1、把log4net.dll添加到项目引用中

2、修改AssemblyInfo.cs,添加如下行:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
3、修改配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    
<configSections>
        
<section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
    
</configSections>
    
<log4net>
        
<appender name="PatternFileAppender" type="SampleAppendersApp.Appender.PatternFileAppender, SampleAppendersApp">
            
<file value="%date{yyyy-MM-dd}\%property{session}\output.log" />
            
<layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
        
</appender>
        
<root>
            
<level value="ALL" />
            
<appender-ref ref="PatternFileAppender" />
        
</root>
    
</log4net>
</configuration>
4、Form1的代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using log4net.Core;

namespace WinFormDemo
{
    
public partial class Form1 : Form
    {
        
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        
private bool logWatching = true;
        
private log4net.Appender.MemoryAppender logger;
        
private Thread logWatcher;


        
public Form1()
        {
            InitializeComponent();

            
this.Closing += new CancelEventHandler(Form1_Closing);
            logger 
= new log4net.Appender.MemoryAppender();

            log4net.Config.BasicConfigurator.Configure(logger);

            logWatcher 
= new Thread(new ThreadStart(LogWatcher));
            logWatcher.Start();

        }

        
void Form1_Closing(object sender, CancelEventArgs e)
        {
            logWatching 
= false;
            logWatcher.Join();

        }

        
delegate void delOneStr(string log);
        
void AppendLog(string _log)
        {
            
if (txtLog.InvokeRequired)
            {
                delOneStr dd 
= new delOneStr(AppendLog);
                txtLog.Invoke(dd, 
new object[] { _log});
            }
            
else
            {                
                StringBuilder builder;
                
if (txtLog.Lines.Length > 99)
                {
                    builder 
= new StringBuilder(txtLog.Text);
                    builder.Remove(
0, txtLog.Text.IndexOf('\r'3000+ 2);
                    builder.Append(_log);
                    txtLog.Clear();
                    txtLog.AppendText(builder.ToString());
                }
                
else
                {
                    txtLog.AppendText(_log);
                }
            }

        }

        
private void LogWatcher()
        {
            
while (logWatching)
            {
                LoggingEvent[] events 
= logger.GetEvents();
                
if (events != null && events.Length > 0)
                {
                    logger.Clear();
                    
foreach (LoggingEvent ev in events)
                    {
                        
string line = ev.LoggerName + "" + ev.RenderedMessage + "\r\n";
                        AppendLog(line);
                    }
                }
                Thread.Sleep(
500);
            }
        }

        
private void timer1_Tick(object sender, EventArgs e)
        {
            Random r 
= new Random();

            
int x = r.Next(14);

            
switch (x)
            {
                
case 1:
                    log.Info(
"通知:通知通知通知");
                    
break;

                
case 2:
                    log.Warn(
"警告:警告警告警告警告警告警告");
                    
break;
                
case 3:
                    log.Error(
"错误:错误错误错误错误错误错误错误错误错误");
                    
break;
                
case 4:
                    log.Fatal(
"严重:严重严重严重严重严重严重严重严重严重严重严重严重严重严重严重严重严重");
                    
break;
            }
        }

    }
}
posted @ 2009-07-01 09:24  Aricc  阅读(3025)  评论(1编辑  收藏  举报