RichTextBoxAppender使用log4net的 [英] RichTextBoxAppender using log4net
本文介绍了RichTextBoxAppender使用log4net的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法以编程方式创建使用log4net的一个RichTextBoxAppender?
换句话说没有XML的app.config
解决方案
使用系统?;使用System.Windows.Forms的
;
使用System.Drawing中;
使用log4net的;
使用log4net.Core;
使用log4net.Appender;
使用log4net.Util;
命名空间Vip.Logging
{
///<总结>
/// RichTextBoxAppender的说明。
///< /总结>
公共类RichTextBoxAppender:AppenderSkeleton
{
#地区的私人实例字段
私人RichTextBox中的RichTextBox = NULL;
私人形式containerForm = NULL;
私人LevelMapping levelMapping =新LevelMapping();
私人INT maxTextLength = 100000;
#endregion
私人委托无效UpdateControlDelegate(LoggingEvent所LoggingEvent所);
#区域构造
公共RichTextBoxAppender(RichTextBox的myRichTextBox):基地()
{
的RichTextBox = myRichTextBox;
containerForm =(表格)richTextBox.Parent;
}
#endregion
私人无效UpdateControl(LoggingEvent所LoggingEvent所)
{
//可能会有性能问题如果缓冲区变得太长
//所以定期清除缓存
如果(richTextBox.TextLength> maxTextLength)
{
richTextBox.Clear();
richTextBox.AppendText(的String.Format((清除日志最大长度:{0})\\\
,maxTextLength));
}
//寻找一个风格映射
LevelTextStyle selectedStyle = levelMapping.Lookup(loggingEvent.Level)为LevelTextStyle;
如果(selectedStyle!= NULL)
{
//设置要被所附
richTextBox.SelectionBackColor = selectedStyle.BackColor文本的颜色;
richTextBox.SelectionColor = selectedStyle.TextColor;
//改变选择字体多达必要
//缺少设置是由在控制
如果(selectedStyle.Font!= NULL)$ B $字体设置的替换b {
//设置字体,大小和样式
richTextBox.SelectionFont = selectedStyle.Font;
}
,否则如果(selectedStyle.PointSize大于0和放大器;&安培;!richTextBox.Font.SizeInPoints = selectedStyle.PointSize)
{
//使用控件的字体,设置大小和样式
浮动大小= selectedStyle.PointSize> 0.0? selectedStyle.PointSize:richTextBox.Font.SizeInPoints;
richTextBox.SelectionFont =新字体(richTextBox.Font.FontFamily.Name,大小,selectedStyle.FontStyle);
}
,否则如果(richTextBox.Font.Style!= selectedStyle.FontStyle)
{
//使用控件的字体和字号,设置样式
richTextBox.SelectionFont =新字体(richTextBox.Font,selectedStyle.FontStyle);
}
}
richTextBox.AppendText(RenderLoggingEvent(LoggingEvent所));
}
保护覆盖无效追加(LoggingEvent所LoggingEvent所)
{
如果(richTextBox.InvokeRequired)
{
richTextBox.Invoke(
新UpdateControlDelegate(UpdateControl),
新的对象[] {LoggingEvent所});
}
,否则
{
UpdateControl(LoggingEvent所);
}
}
公共无效AddMapping(LevelTextStyle映射)
{
levelMapping.Add(映射);
}
公共覆盖无效ActivateOptions()
{
base.ActivateOptions();
levelMapping.ActivateOptions();
}
保护覆盖布尔RequiresLayout {获得{返回true; }}
}
公共类LevelTextStyle:LevelMappingEntry
{
私人彩色文字颜色;
私人颜色背景色;
私人fontstyle的fontstyle的= FontStyle.Regular;
私人浮动的pointsize = 0.0;
私人布尔大胆= FALSE;
私人布尔斜体= FALSE;
私人字符串fontFamilyName = NULL;
私人字体的字体= NULL;
公共BOOL大胆{{返回大胆; } {设定大胆=价值; }}
公共BOOL斜体{{返回斜体; } {设置斜体=价值; }}
公众持股量的pointsize {{返回的pointsize; } {设置=的pointsize价值; }}
///<总结>
///初始化的对象
选项///< /总结>
///<&言论GT;解析性质和LT; /言论>
公共覆盖无效ActivateOptions()
{
base.ActivateOptions();
如果(粗体)fontstyle的| = FontStyle.Bold;
如果(斜体)fontstyle的| = FontStyle.Italic;
如果(fontFamilyName!= NULL)
{
浮动大小=的pointsize> 0.0?的pointsize:8.25f;
试
{
字体=新字体(fontFamilyName,大小fontstyle的);
}
赶上(例外)
{
字体=新的字体(宋体,8.25f,FontStyle.Regular);
}
}
}
众彩TEXTCOLOR {{返回文字颜色; }集合{文字颜色=价值; }}
公共颜色的BackColor {{返回背景色; } {设置背景色=价值; }}
公共fontstyle的fontstyle的{{返回fontstyle的; }集合{fontstyle的=价值; }}
公共字体字体{{返回字体; } {设置字体=价值; }}
}
}
公共部分类的MainForm:表格
{
私人静态字符串LOCPATH = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
私人静态字符串dskPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
私人RichTextBoxAppender RBA;
私人MessageBoxAppender MBA;
公众的MainForm()
{
的InitializeComponent();
如果(!Global.logger.Logger.Repository.Configured)
{
RBA =新RichTextBoxAppender(richTextBoxLog);
rba.Threshold = Level.ALL的;
rba.Layout =新的PatternLayout(%DATE {DD-MM-YYYY HH:MM:SS.FFF}%5level%讯息%N);
LevelTextStyle ILTS =新LevelTextStyle();
ilts.Level = Level.Info;
ilts.TextColor = Color.Yellow;
ilts.PointSize = 10.0f;
rba.AddMapping(ILTS);
LevelTextStyle DLTS =新LevelTextStyle();
dlts.Level = Level.Debug;
dlts.TextColor = Color.LightBlue;
dlts.PointSize = 10.0f;
rba.AddMapping(DLTS);
LevelTextStyle wlts =新LevelTextStyle();
wlts.Level = Level.Warn;
wlts.TextColor = Color.Chartreuse;
wlts.PointSize = 10.0f;
rba.AddMapping(wlts);
LevelTextStyle的ELT =新LevelTextStyle();
elts.Level = Level.Error;
elts.TextColor = Color.Crimson;
elts.BackColor = Color.Cornsilk;
elts.PointSize = 10.0f;
rba.AddMapping(应急定位发射机);
BasicConfigurator.Configure(RBA);
rba.ActivateOptions();
MBA =新MessageBoxAppender();
mba.Layout =新的PatternLayout(%DATE {DD-MM-YYYY HH:MM:SS.FFF}%5level%讯息%N);
mba.Threshold = Level.Error;
BasicConfigurator.Configure(MBA);
mba.ActivateOptions();
RollingFileAppender进行FA =新RollingFileAppender进行();
fa.AppendToFile = TRUE;
fa.Threshold = log4net.Core.Level.All;
fa.RollingStyle = RollingFileAppender.RollingMode.Size;
fa.MaxFileSize = 100000;
fa.MaxSizeRollBackups = 3;
fa.File = dskPath + @\FgPleoraLog.txt
fa.Layout =新log4net.Layout.PatternLayout(%DATE {DD-MM-YYYY HH:MM:SS.FFF}%5level%消息(%记录器{1}:%线)%N) ;
log4net.Config.BasicConfigurator.Configure(FA);
fa.ActivateOptions();
}
}
Is there a way to programatically create a RichTextBoxAppender using log4net? In other words no xml app.config?
解决方案
using System;
using System.Windows.Forms;
using System.Drawing;
using log4net;
using log4net.Core;
using log4net.Appender;
using log4net.Util;
namespace Vip.Logging
{
/// <summary>
/// Description of RichTextBoxAppender.
/// </summary>
public class RichTextBoxAppender : AppenderSkeleton
{
#region Private Instance Fields
private RichTextBox richTextBox = null;
private Form containerForm = null;
private LevelMapping levelMapping = new LevelMapping();
private int maxTextLength = 100000;
#endregion
private delegate void UpdateControlDelegate(LoggingEvent loggingEvent);
#region Constructor
public RichTextBoxAppender(RichTextBox myRichTextBox) : base()
{
richTextBox = myRichTextBox;
containerForm = (Form)richTextBox.Parent;
}
#endregion
private void UpdateControl(LoggingEvent loggingEvent)
{
// There may be performance issues if the buffer gets too long
// So periodically clear the buffer
if (richTextBox.TextLength > maxTextLength)
{
richTextBox.Clear();
richTextBox.AppendText(string.Format("(Cleared log length max: {0})\n", maxTextLength));
}
// look for a style mapping
LevelTextStyle selectedStyle = levelMapping.Lookup(loggingEvent.Level) as LevelTextStyle;
if (selectedStyle != null)
{
// set the colors of the text about to be appended
richTextBox.SelectionBackColor = selectedStyle.BackColor;
richTextBox.SelectionColor = selectedStyle.TextColor;
// alter selection font as much as necessary
// missing settings are replaced by the font settings on the control
if (selectedStyle.Font != null)
{
// set Font Family, size and styles
richTextBox.SelectionFont = selectedStyle.Font;
}
else if (selectedStyle.PointSize > 0 && richTextBox.Font.SizeInPoints != selectedStyle.PointSize)
{
// use control's font family, set size and styles
float size = selectedStyle.PointSize > 0.0f ? selectedStyle.PointSize : richTextBox.Font.SizeInPoints;
richTextBox.SelectionFont = new Font(richTextBox.Font.FontFamily.Name, size, selectedStyle.FontStyle);
}
else if (richTextBox.Font.Style != selectedStyle.FontStyle)
{
// use control's font family and size, set styles
richTextBox.SelectionFont = new Font(richTextBox.Font, selectedStyle.FontStyle);
}
}
richTextBox.AppendText(RenderLoggingEvent(loggingEvent));
}
protected override void Append(LoggingEvent LoggingEvent)
{
if (richTextBox.InvokeRequired)
{
richTextBox.Invoke(
new UpdateControlDelegate(UpdateControl),
new object[] { LoggingEvent });
}
else
{
UpdateControl(LoggingEvent);
}
}
public void AddMapping(LevelTextStyle mapping)
{
levelMapping.Add(mapping);
}
public override void ActivateOptions()
{
base.ActivateOptions();
levelMapping.ActivateOptions();
}
protected override bool RequiresLayout { get { return true; } }
}
public class LevelTextStyle : LevelMappingEntry
{
private Color textColor;
private Color backColor;
private FontStyle fontStyle = FontStyle.Regular;
private float pointSize = 0.0f;
private bool bold = false;
private bool italic = false;
private string fontFamilyName = null;
private Font font = null;
public bool Bold { get { return bold; } set { bold = value; } }
public bool Italic { get { return italic; } set { italic = value; } }
public float PointSize { get { return pointSize; } set { pointSize = value; } }
/// <summary>
/// Initialize the options for the object
/// </summary>
/// <remarks>Parse the properties</remarks>
public override void ActivateOptions()
{
base.ActivateOptions();
if (bold) fontStyle |= FontStyle.Bold;
if (italic) fontStyle |= FontStyle.Italic;
if (fontFamilyName != null)
{
float size = pointSize > 0.0f ? pointSize : 8.25f;
try
{
font = new Font(fontFamilyName, size, fontStyle);
}
catch (Exception)
{
font = new Font("Arial", 8.25f, FontStyle.Regular);
}
}
}
public Color TextColor { get { return textColor; } set { textColor = value; } }
public Color BackColor { get { return backColor; } set { backColor = value; } }
public FontStyle FontStyle { get { return fontStyle; } set { fontStyle = value; } }
public Font Font { get { return font; } set { font = value; } }
}
}
public partial class MainForm : Form
{
private static string locPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
private static string dskPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
private RichTextBoxAppender rba;
private MessageBoxAppender mba;
public MainForm()
{
InitializeComponent();
if (!Global.logger.Logger.Repository.Configured)
{
rba = new RichTextBoxAppender(richTextBoxLog);
rba.Threshold = Level.All;
rba.Layout = new PatternLayout("%date{dd-MM-yyyy HH:mm:ss.fff} %5level %message %n");
LevelTextStyle ilts = new LevelTextStyle();
ilts.Level = Level.Info;
ilts.TextColor = Color.Yellow;
ilts.PointSize = 10.0f;
rba.AddMapping(ilts);
LevelTextStyle dlts = new LevelTextStyle();
dlts.Level = Level.Debug;
dlts.TextColor = Color.LightBlue;
dlts.PointSize = 10.0f;
rba.AddMapping(dlts);
LevelTextStyle wlts = new LevelTextStyle();
wlts.Level = Level.Warn;
wlts.TextColor = Color.Chartreuse;
wlts.PointSize = 10.0f;
rba.AddMapping(wlts);
LevelTextStyle elts = new LevelTextStyle();
elts.Level = Level.Error;
elts.TextColor = Color.Crimson;
elts.BackColor = Color.Cornsilk;
elts.PointSize = 10.0f;
rba.AddMapping(elts);
BasicConfigurator.Configure(rba);
rba.ActivateOptions();
mba = new MessageBoxAppender();
mba.Layout = new PatternLayout("%date{dd-MM-yyyy HH:mm:ss.fff} %5level %message %n");
mba.Threshold = Level.Error;
BasicConfigurator.Configure(mba);
mba.ActivateOptions();
RollingFileAppender fa = new RollingFileAppender();
fa.AppendToFile = true;
fa.Threshold = log4net.Core.Level.All;
fa.RollingStyle = RollingFileAppender.RollingMode.Size;
fa.MaxFileSize = 100000;
fa.MaxSizeRollBackups = 3;
fa.File = dskPath + @"\FgPleoraLog.txt";
fa.Layout = new log4net.Layout.PatternLayout("%date{dd-MM-yyyy HH:mm:ss.fff} %5level %message (%logger{1}:%line)%n");
log4net.Config.BasicConfigurator.Configure(fa);
fa.ActivateOptions();
}
}
这篇关于RichTextBoxAppender使用log4net的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文