我正在寻找有效实现Windows窗体应用程序日志窗口的方法。在过去,我使用TextBox和RichTextBox实现了几个,但我仍然不满意这个功能。
此日志旨在向用户提供各种事件的近期历史记录,主要用于数据收集应用程序,其中可能会对特定事务的完成方式感到好奇。在这种情况下,日志不必是永久性的,也不必保存到文件中。
首先,提出了一些要求:
到目前为止我一直用来编写和修剪日志:
我使用以下代码(我从其他线程调用):
// rtbLog is a RichTextBox
// _MaxLines is an int
public void AppendLog(string s, Color c, bool bNewLine)
{
if (rtbLog.InvokeRequired)
{
object[] args = { s, c, bNewLine };
rtbLog.Invoke(new AppendLogDel(AppendLog), args);
return;
}
try
{
rtbLog.SelectionColor = c;
rtbLog.AppendText(s);
if (bNewLine) rtbLog.AppendText(Environment.NewLine);
TrimLog();
rtbLog.SelectionStart = rtbLog.TextLength;
rtbLog.ScrollToCaret();
rtbLog.Update();
}
catch (Exception exc)
{
// exception handling
}
}
private void TrimLog()
{
try
{
// Extra lines as buffer to save time
if (rtbLog.Lines.Length < _MaxLines + 10)
{
return;
}
else
{
string[] sTemp = rtxtLog.Lines;
string[] sNew= new string[_MaxLines];
int iLineOffset = sTemp.Length - _MaxLines;
for (int n = 0; n < _MaxLines; n++)
{
sNew[n] = sTemp[iLineOffset];
iLineOffset++;
}
rtbLog.Lines = sNew;
}
}
catch (Exception exc)
{
// exception handling
}
}
这种方法的问题是每当调用TrimLog时,我都会丢失颜色格式。使用常规TextBox,这样可以正常工作(当然需要稍加修改)。
寻找解决方案从未真正令人满意。有人建议在RichTextBox中按字符数而不是行计数来修剪多余的部分。我也见过ListBoxes,但还没有成功试过。
答案 0 :(得分:25)
这是我基于我前一段时间写的更复杂的记录器而扔在一起的东西。
这将根据日志级别支持列表框中的颜色,支持Ctrl + V和右键单击以复制为RTF,并处理从其他线程到ListBox的日志记录。
您可以使用其中一个构造函数重载覆盖ListBox中保留的行数(默认为2000)以及消息格式。
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Text;
namespace StackOverflow
{
public partial class Main : Form
{
public static ListBoxLog listBoxLog;
public Main()
{
InitializeComponent();
listBoxLog = new ListBoxLog(listBox1);
Thread thread = new Thread(LogStuffThread);
thread.IsBackground = true;
thread.Start();
}
private void LogStuffThread()
{
int number = 0;
while (true)
{
listBoxLog.Log(Level.Info, "A info level message from thread # {0,0000}", number++);
Thread.Sleep(2000);
}
}
private void button1_Click(object sender, EventArgs e)
{
listBoxLog.Log(Level.Debug, "A debug level message");
}
private void button2_Click(object sender, EventArgs e)
{
listBoxLog.Log(Level.Verbose, "A verbose level message");
}
private void button3_Click(object sender, EventArgs e)
{
listBoxLog.Log(Level.Info, "A info level message");
}
private void button4_Click(object sender, EventArgs e)
{
listBoxLog.Log(Level.Warning, "A warning level message");
}
private void button5_Click(object sender, EventArgs e)
{
listBoxLog.Log(Level.Error, "A error level message");
}
private void button6_Click(object sender, EventArgs e)
{
listBoxLog.Log(Level.Critical, "A critical level message");
}
private void button7_Click(object sender, EventArgs e)
{
listBoxLog.Paused = !listBoxLog.Paused;
}
}
public enum Level : int
{
Critical = 0,
Error = 1,
Warning = 2,
Info = 3,
Verbose = 4,
Debug = 5
};
public sealed class ListBoxLog : IDisposable
{
private const string DEFAULT_MESSAGE_FORMAT = "{0} [{5}] : {8}";
private const int DEFAULT_MAX_LINES_IN_LISTBOX = 2000;
private bool _disposed;
private ListBox _listBox;
private string _messageFormat;
private int _maxEntriesInListBox;
private bool _canAdd;
private bool _paused;
private void OnHandleCreated(object sender, EventArgs e)
{
_canAdd = true;
}
private void OnHandleDestroyed(object sender, EventArgs e)
{
_canAdd = false;
}
private void DrawItemHandler(object sender, DrawItemEventArgs e)
{
if (e.Index >= 0)
{
e.DrawBackground();
e.DrawFocusRectangle();
LogEvent logEvent = ((ListBox)sender).Items[e.Index] as LogEvent;
// SafeGuard against wrong configuration of list box
if (logEvent == null)
{
logEvent = new LogEvent(Level.Critical, ((ListBox)sender).Items[e.Index].ToString());
}
Color color;
switch (logEvent.Level)
{
case Level.Critical:
color = Color.White;
break;
case Level.Error:
color = Color.Red;
break;
case Level.Warning:
color = Color.Goldenrod;
break;
case Level.Info:
color = Color.Green;
break;
case Level.Verbose:
color = Color.Blue;
break;
default:
color = Color.Black;
break;
}
if (logEvent.Level == Level.Critical)
{
e.Graphics.FillRectangle(new SolidBrush(Color.Red), e.Bounds);
}
e.Graphics.DrawString(FormatALogEventMessage(logEvent, _messageFormat), new Font("Lucida Console", 8.25f, FontStyle.Regular), new SolidBrush(color), e.Bounds);
}
}
private void KeyDownHandler(object sender, KeyEventArgs e)
{
if ((e.Modifiers == Keys.Control) && (e.KeyCode == Keys.C))
{
CopyToClipboard();
}
}
private void CopyMenuOnClickHandler(object sender, EventArgs e)
{
CopyToClipboard();
}
private void CopyMenuPopupHandler(object sender, EventArgs e)
{
ContextMenu menu = sender as ContextMenu;
if (menu != null)
{
menu.MenuItems[0].Enabled = (_listBox.SelectedItems.Count > 0);
}
}
private class LogEvent
{
public LogEvent(Level level, string message)
{
EventTime = DateTime.Now;
Level = level;
Message = message;
}
public readonly DateTime EventTime;
public readonly Level Level;
public readonly string Message;
}
private void WriteEvent(LogEvent logEvent)
{
if ((logEvent != null) && (_canAdd))
{
_listBox.BeginInvoke(new AddALogEntryDelegate(AddALogEntry), logEvent);
}
}
private delegate void AddALogEntryDelegate(object item);
private void AddALogEntry(object item)
{
_listBox.Items.Add(item);
if (_listBox.Items.Count > _maxEntriesInListBox)
{
_listBox.Items.RemoveAt(0);
}
if (!_paused) _listBox.TopIndex = _listBox.Items.Count - 1;
}
private string LevelName(Level level)
{
switch (level)
{
case Level.Critical: return "Critical";
case Level.Error: return "Error";
case Level.Warning: return "Warning";
case Level.Info: return "Info";
case Level.Verbose: return "Verbose";
case Level.Debug: return "Debug";
default: return string.Format("<value={0}>", (int)level);
}
}
private string FormatALogEventMessage(LogEvent logEvent, string messageFormat)
{
string message = logEvent.Message;
if (message == null) { message = "<NULL>"; }
return string.Format(messageFormat,
/* {0} */ logEvent.EventTime.ToString("yyyy-MM-dd HH:mm:ss.fff"),
/* {1} */ logEvent.EventTime.ToString("yyyy-MM-dd HH:mm:ss"),
/* {2} */ logEvent.EventTime.ToString("yyyy-MM-dd"),
/* {3} */ logEvent.EventTime.ToString("HH:mm:ss.fff"),
/* {4} */ logEvent.EventTime.ToString("HH:mm:ss"),
/* {5} */ LevelName(logEvent.Level)[0],
/* {6} */ LevelName(logEvent.Level),
/* {7} */ (int)logEvent.Level,
/* {8} */ message);
}
private void CopyToClipboard()
{
if (_listBox.SelectedItems.Count > 0)
{
StringBuilder selectedItemsAsRTFText = new StringBuilder();
selectedItemsAsRTFText.AppendLine(@"{\rtf1\ansi\deff0{\fonttbl{\f0\fcharset0 Courier;}}");
selectedItemsAsRTFText.AppendLine(@"{\colortbl;\red255\green255\blue255;\red255\green0\blue0;\red218\green165\blue32;\red0\green128\blue0;\red0\green0\blue255;\red0\green0\blue0}");
foreach (LogEvent logEvent in _listBox.SelectedItems)
{
selectedItemsAsRTFText.AppendFormat(@"{{\f0\fs16\chshdng0\chcbpat{0}\cb{0}\cf{1} ", (logEvent.Level == Level.Critical) ? 2 : 1, (logEvent.Level == Level.Critical) ? 1 : ((int)logEvent.Level > 5) ? 6 : ((int)logEvent.Level) + 1);
selectedItemsAsRTFText.Append(FormatALogEventMessage(logEvent, _messageFormat));
selectedItemsAsRTFText.AppendLine(@"\par}");
}
selectedItemsAsRTFText.AppendLine(@"}");
System.Diagnostics.Debug.WriteLine(selectedItemsAsRTFText.ToString());
Clipboard.SetData(DataFormats.Rtf, selectedItemsAsRTFText.ToString());
}
}
public ListBoxLog(ListBox listBox) : this(listBox, DEFAULT_MESSAGE_FORMAT, DEFAULT_MAX_LINES_IN_LISTBOX) { }
public ListBoxLog(ListBox listBox, string messageFormat) : this(listBox, messageFormat, DEFAULT_MAX_LINES_IN_LISTBOX) { }
public ListBoxLog(ListBox listBox, string messageFormat, int maxLinesInListbox)
{
_disposed = false;
_listBox = listBox;
_messageFormat = messageFormat;
_maxEntriesInListBox = maxLinesInListbox;
_paused = false;
_canAdd = listBox.IsHandleCreated;
_listBox.SelectionMode = SelectionMode.MultiExtended;
_listBox.HandleCreated += OnHandleCreated;
_listBox.HandleDestroyed += OnHandleDestroyed;
_listBox.DrawItem += DrawItemHandler;
_listBox.KeyDown += KeyDownHandler;
MenuItem[] menuItems = new MenuItem[] { new MenuItem("Copy", new EventHandler(CopyMenuOnClickHandler)) };
_listBox.ContextMenu = new ContextMenu(menuItems);
_listBox.ContextMenu.Popup += new EventHandler(CopyMenuPopupHandler);
_listBox.DrawMode = DrawMode.OwnerDrawFixed;
}
public void Log(string message) { Log(Level.Debug, message); }
public void Log(string format, params object[] args) { Log(Level.Debug, (format == null) ? null : string.Format(format, args)); }
public void Log(Level level, string format, params object[] args) { Log(level, (format == null) ? null : string.Format(format, args)); }
public void Log(Level level, string message)
{
WriteEvent(new LogEvent(level, message));
}
public bool Paused
{
get { return _paused; }
set { _paused = value; }
}
~ListBoxLog()
{
if (!_disposed)
{
Dispose(false);
_disposed = true;
}
}
public void Dispose()
{
if (!_disposed)
{
Dispose(true);
GC.SuppressFinalize(this);
_disposed = true;
}
}
private void Dispose(bool disposing)
{
if (_listBox != null)
{
_canAdd = false;
_listBox.HandleCreated -= OnHandleCreated;
_listBox.HandleCreated -= OnHandleDestroyed;
_listBox.DrawItem -= DrawItemHandler;
_listBox.KeyDown -= KeyDownHandler;
_listBox.ContextMenu.MenuItems.Clear();
_listBox.ContextMenu.Popup -= CopyMenuPopupHandler;
_listBox.ContextMenu = null;
_listBox.Items.Clear();
_listBox.DrawMode = DrawMode.Normal;
_listBox = null;
}
}
}
}
答案 1 :(得分:24)
我建议您根本不使用控件作为日志。而是编写一个具有所需属性的日志集合类(不包括显示属性)。
然后编写将该集合转储到各种用户界面元素所需的一些代码。就个人而言,我会将SendToEditControl
和SendToListBox
方法放入我的日志记录对象中。我可能会为这些方法添加过滤功能。
您可以仅在有意义的情况下更新UI日志,为您提供最佳性能,更重要的是,让您在日志快速变化时减少UI开销。
重要的是不要将您的日志记录绑定到UI,这是一个错误。总有一天,你可能想要无头跑。
从长远来看,记录器的良好UI可能是一个自定义控件。但在短期内,您只想将日志记录与任何特定的 UI断开连接。
答案 2 :(得分:12)
当我想使用RichTextBox再次记录彩色线条时,我会将此存储为Future Me的帮助。以下代码删除了RichTextBox中的第一行:
if ( logTextBox.Lines.Length > MAX_LINES )
{
logTextBox.Select(0, logTextBox.Text.IndexOf('\n')+1);
logTextBox.SelectedRtf = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1053\\uc1 }";
}
我花了太长时间才弄清楚将SelectedRtf设置为“”不起作用,但将其设置为“正确”的RTF并且没有文本内容是可以的。
答案 3 :(得分:5)
我最近实施了类似的东西。我们的方法是保留回滚记录的环形缓冲区,并手动绘制日志文本(使用Graphics.DrawString)。然后,如果用户想要向后滚动,复制文本等,我们有一个“暂停”按钮,可以翻转回普通的TextBox控件。
答案 4 :(得分:3)
我认为ListView非常适合这种情况(在详细信息查看模式下),它正是我在一些内部应用程序中使用它的。
有用提示:如果您知道一次添加/删除大量项目,请使用BeginUpdate()和EndUpdate()。
答案 5 :(得分:2)
如果你想要突出显示和颜色格式化,我建议使用RichTextBox。
如果您想要自动滚动,请使用ListBox。
在任何一种情况下,将其绑定到行的循环缓冲区。
答案 6 :(得分:1)
我创建基本日志窗口的解决方案与John Knoeller完全相同。避免将日志信息直接存储在TextBox或RichTextBox控件中,而是创建一个可用于填充控件或写入文件等的日志记录类。
此示例解决方案包括以下几部分:
Logger
。ScrollingRichTextBox
。LoggerExample
。首先,记录类:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
namespace Logger
{
/// <summary>
/// A circular buffer style logging class which stores N items for display in a Rich Text Box.
/// </summary>
public class Logger
{
private readonly Queue<LogEntry> _log;
private uint _entryNumber;
private readonly uint _maxEntries;
private readonly object _logLock = new object();
private readonly Color _defaultColor = Color.White;
private class LogEntry
{
public uint EntryId;
public DateTime EntryTimeStamp;
public string EntryText;
public Color EntryColor;
}
private struct ColorTableItem
{
public uint Index;
public string RichColor;
}
/// <summary>
/// Create an instance of the Logger class which stores <paramref name="maximumEntries"/> log entries.
/// </summary>
public Logger(uint maximumEntries)
{
_log = new Queue<LogEntry>();
_maxEntries = maximumEntries;
}
/// <summary>
/// Retrieve the contents of the log as rich text, suitable for populating a <see cref="System.Windows.Forms.RichTextBox.Rtf"/> property.
/// </summary>
/// <param name="includeEntryNumbers">Option to prepend line numbers to each entry.</param>
public string GetLogAsRichText(bool includeEntryNumbers)
{
lock (_logLock)
{
var sb = new StringBuilder();
var uniqueColors = BuildRichTextColorTable();
sb.AppendLine($@"{{\rtf1{{\colortbl;{ string.Join("", uniqueColors.Select(d => d.Value.RichColor)) }}}");
foreach (var entry in _log)
{
if (includeEntryNumbers)
sb.Append($"\\cf1 { entry.EntryId }. ");
sb.Append($"\\cf1 { entry.EntryTimeStamp.ToShortDateString() } { entry.EntryTimeStamp.ToShortTimeString() }: ");
var richColor = $"\\cf{ uniqueColors[entry.EntryColor].Index + 1 }";
sb.Append($"{ richColor } { entry.EntryText }\\par").AppendLine();
}
return sb.ToString();
}
}
/// <summary>
/// Adds <paramref name="text"/> as a log entry.
/// </summary>
public void AddToLog(string text)
{
AddToLog(text, _defaultColor);
}
/// <summary>
/// Adds <paramref name="text"/> as a log entry, and specifies a color to display it in.
/// </summary>
public void AddToLog(string text, Color entryColor)
{
lock (_log)
{
if (_entryNumber >= uint.MaxValue)
_entryNumber = 0;
_entryNumber++;
var logEntry = new LogEntry { EntryId = _entryNumber, EntryTimeStamp = DateTime.Now, EntryText = text, EntryColor = entryColor };
_log.Enqueue(logEntry);
while (_log.Count > _maxEntries)
_log.Dequeue();
}
}
/// <summary>
/// Clears the entire log.
/// </summary>
public void Clear()
{
lock (_logLock)
{
_log.Clear();
}
}
private Dictionary<Color, ColorTableItem> BuildRichTextColorTable()
{
var uniqueColors = new Dictionary<Color, ColorTableItem>();
var index = 0u;
uniqueColors.Add(_defaultColor, new ColorTableItem() { Index = index++, RichColor = ColorToRichColorString(_defaultColor) });
foreach (var c in _log.Select(l => l.EntryColor).Distinct().Where(c => c != _defaultColor))
uniqueColors.Add(c, new ColorTableItem() { Index = index++, RichColor = ColorToRichColorString(c) });
return uniqueColors;
}
private string ColorToRichColorString(Color c)
{
return $"\\red{c.R}\\green{c.G}\\blue{c.B};";
}
}
}
Logger类包含另一个类LogEntry
,该类跟踪行号,时间戳和所需的颜色。一种结构用于构建Rich Text颜色表。
接下来,这是修改后的RichTextBox:
using System;
using System.Runtime.InteropServices;
namespace Logger
{
public class ScrollingRichTextBox : System.Windows.Forms.RichTextBox
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(
IntPtr hWnd,
uint Msg,
IntPtr wParam,
IntPtr LParam);
private const int _WM_VSCROLL = 277;
private const int _SB_BOTTOM = 7;
/// <summary>
/// Scrolls to the bottom of the RichTextBox.
/// </summary>
public void ScrollToBottom()
{
SendMessage(Handle, _WM_VSCROLL, new IntPtr(_SB_BOTTOM), new IntPtr(0));
}
}
}
我在这里所做的就是继承RichTextBox并添加“从底部滚动”方法。关于如何在StackOverflow上执行此操作还有其他问题,我从中得出了这种方法。
最后,是在表单中使用此类的示例:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Logger
{
public partial class LoggerExample : Form
{
private Logger _log = new Logger(100u);
private List<Color> _randomColors = new List<Color> { Color.Red, Color.SkyBlue, Color.Green };
private Random _r = new Random((int)DateTime.Now.Ticks);
public LoggerExample()
{
InitializeComponent();
}
private void timerGenerateText_Tick(object sender, EventArgs e)
{
if (_r.Next(10) > 5)
_log.AddToLog("Some event to log.", _randomColors[_r.Next(3)]);
}
private void timeUpdateLogWindow_Tick(object sender, EventArgs e)
{
richTextBox1.Rtf = _log.GetLogAsRichText(true);
richTextBox1.ScrollToBottom();
}
}
}
此表单由两个计时器创建,一个计时器用于伪随机地生成日志条目,另一个计时器用于填充RichTextBox本身。在此示例中,使用100行回滚实例化日志类。 RichTextBox控件的颜色设置为具有白色和各种颜色前景的黑色背景。生成文本的计时器间隔为100毫秒,而更新日志窗口的计时器间隔为1000毫秒。
示例输出:
这还远未达到完美或完成的水平,但是这里有一些警告和需要补充或改进的地方(其中一些我在以后的项目中已经完成):
maximumEntries
的值较大,则性能会很差。该日志记录类仅设计用于数百行的回滚。可以随时修改和改进此示例。欢迎提供反馈。