private void UpdateControl(LoggingEvent loggingEvent) { if (!richTextBox.Visible) { return; } // There may be performance issues if the buffer gets too long // So periodically clear the buffer if (richTextBox.TextLength > maxTextLength) { //richTextBox.Clear(); //richTextBox.SuspendLayout(); while (richTextBox.TextLength > (int)(maxTextLength * 0.9)) { richTextBox.Select(0, richTextBox.GetFirstCharIndexFromLine(1)); // select the first line richTextBox.SelectedText = ""; } //richTextBox.AppendText(string.Format("(Cleared log length max: {0})\n", maxTextLength)); //richTextBox.ScrollToCaret(); //richTextBox.ResumeLayout(); } // 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)); richTextBox.ScrollToCaret(); }
public void AddMapping(LevelTextStyle mapping) { levelMapping.Add(mapping); }
/// <summary> /// Initialisiert das Logging und die Debug TextBox /// </summary> private void InitLog() { #region RichTextBoxAppender RichTextBoxAppender rba = new RichTextBoxAppender(rtbDebug); rba.Threshold = Level.All; rba.Layout = new log4net.Layout.PatternLayout("%date{dd-MM-yyyy HH:mm:ss.fff} %5level %message %n"); LevelTextStyle ilts = new LevelTextStyle(); ilts.Level = Level.Info; ilts.TextColor = Color.DarkBlue; ilts.PointSize = 8.0f; rba.AddMapping(ilts); LevelTextStyle dlts = new LevelTextStyle(); dlts.Level = Level.Debug; dlts.TextColor = Color.DeepSkyBlue; dlts.PointSize = 8.0f; rba.AddMapping(dlts); LevelTextStyle wlts = new LevelTextStyle(); wlts.Level = Level.Warn; wlts.TextColor = Color.Gold; wlts.PointSize = 8.0f; rba.AddMapping(wlts); LevelTextStyle elts = new LevelTextStyle(); elts.Level = Level.Error; elts.TextColor = Color.Crimson; elts.BackColor = Color.Cornsilk; elts.PointSize = 8.0f; rba.AddMapping(elts); LevelTextStyle flts = new LevelTextStyle(); flts.Level = Level.Fatal; flts.TextColor = Color.White; flts.BackColor = Color.Red; flts.PointSize = 8.0f; rba.AddMapping(flts); log4net.Config.BasicConfigurator.Configure(rba); rba.ActivateOptions(); #endregion RichTextBoxAppender #region RollingFileAppender log4net.Appender.RollingFileAppender fa = new log4net.Appender.RollingFileAppender(); fa.AppendToFile = true; fa.Threshold = log4net.Core.Level.All; fa.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date; fa.MaxFileSize = 100000; fa.MaxSizeRollBackups = 10; fa.File = @".\LogPath\K-S2-Remote_"; fa.DatePattern = @"yyyyMMdd.lo\g"; fa.StaticLogFileName = false; // fa.Layout = new log4net.Layout.PatternLayout("%date{dd-MM-yyyy HH:mm:ss.fff} %5level %message (%logger{1}:%line)%n"); log4net.Layout.PatternLayout pl = new log4net.Layout.PatternLayout( "%date [%thread] %-5level %message (%logger)%newline"); pl.Header = "######### K-S2-Remote_ gestartet ############\n"; pl.Footer = "######### K-S2-Remote_ gestoppt ############\n"; fa.Layout = pl; log4net.Config.BasicConfigurator.Configure(fa); fa.ActivateOptions(); #endregion RollingFileAppender }