WinForms C# 中优雅的日志窗口 [英] Elegant Log Window in WinForms C#

查看:22
本文介绍了WinForms C# 中优雅的日志窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关为 Windows 窗体应用程序实现日志窗口的有效方法的想法.过去我已经使用 TextBox 和 RichTextBox 实现了几个,但我仍然对功能不完全满意.

此日志旨在为用户提供各种事件的近期历史记录,主要用于数据收集应用程序,在这些应用程序中,人们可能会好奇特定事务是如何完成的.在这种情况下,日志不需要是永久的,也不需要保存到文件中.

This log is intended to provide the user with a recent history of various events, primarily used in data-gathering applications where one might be curious how a particular transaction completed. In this case, the log need not be permanent nor saved to a file.

首先,一些提议的要求:

First, some proposed requirements:

  • 高效快速;如果将数百行快速连续写入日志,则需要消耗最少的资源和时间.
  • 能够提供多达 2000 行左右的可变回滚.任何更长的时间都是不必要的.
  • 突出显示和颜色是首选.不需要字体效果.
  • 在达到回滚限制时自动修剪线条.
  • 在添加新数据时自动滚动.
  • 奖励但不是必需的:在手动交互(例如用户正在浏览历史记录)期间暂停自动滚动.

到目前为止我一直在使用什么来编写和修剪日志:

What I have been using so far to write and trim the log:

我使用以下代码(我从其他线程调用):

I use the following code (which I call from other threads):

// 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 就可以了(当然需要稍加修改).

The problem with this approach is that whenever TrimLog is called, I lose color formatting. With a regular TextBox this works just fine (with a bit of modification of course).

对此问题的解决方案的搜索从未真正令人满意.有些人建议在 RichTextBox 中通过字符数而不是行数来修剪多余的部分.我也看到过使用 ListBoxes,但没有成功尝试过.

Searches for a solution to this have never been really satisfactory. Some suggest to trim the excess by character count instead of line count in a RichTextBox. I've also seen ListBoxes used, but haven't successfully tried it.

推荐答案

我建议您根本不要使用控件作为日志.而是编写一个日志 collection 类,该类具有您想要的属性(不包括显示属性).

I recommend that you don't use a control as your log at all. Instead write a log collection class that has the properties you desire (not including the display properties).

然后编写将集合转储到各种用户界面元素所需的少量代码.就个人而言,我会将 SendToEditControlSendToListBox 方法放入我的日志记录对象中.我可能会为这些方法添加过滤功能.

Then write the little bit of code that is needed to dump that collection to a variety of user interface elements. Personally, I would put SendToEditControl and SendToListBox methods into my logging object. I would probably add filtering capabilities to these methods.

您可以仅在有意义的情况下更新 UI 日志,从而为您提供最佳性能,更重要的是,当日志快速更改时,您可以减少 UI 开销.

You can update the UI log only as often as it makes sense, giving you the best possible performance, and more importantly, letting you reduce the UI overhead when the log is changing rapidly.

重要的是不要将您的日志记录与某个 UI 联系起来,这是一个错误.有一天你可能想要无头奔跑.

The important thing is not to tie your logging to a piece of UI, that's a mistake. Someday you may want to run headless.

从长远来看,一个好的记录器 UI 可能是一个自定义控件.但在短期内,您只想将日志记录与任何特定 UI 部分断开.

In the long run, a good UI for a logger is probably a custom control. But in the short run, you just want to disconnect your logging from any specific piece of UI.

这篇关于WinForms C# 中优雅的日志窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆