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 log4net_richtextbox
{
public partial class Form2 : 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 Form2()
{
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)
{
log.Info(DateTime.Now.ToString());
}
}
}
log4net异步刷新 TextBox (试运行版)
最新推荐文章于 2020-09-02 17:51:21 发布