237 lines
8.2 KiB
C#
237 lines
8.2 KiB
C#
|
|
using XKRS.UI.Model.Winform;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using static DH.Commons.Enums.EnumHelper;
|
|
using DH.Commons.Enums;
|
|
|
|
|
|
namespace DHSoftware.Views
|
|
{
|
|
|
|
public partial class FrmLog : UserControl
|
|
{
|
|
public FrmLog()
|
|
{
|
|
InitializeComponent();
|
|
|
|
lvLog.ShowItemToolTips = true;
|
|
|
|
this.Load += (s, e) =>
|
|
{
|
|
_showLevels.Clear();
|
|
tsmiLogLevels.DropDownItems.Clear();
|
|
JsonConvert.DeserializeObject<List<dynamic>>(JsonConvert.SerializeObject(EnumHelper.GetEnumListByType(typeof(LogLevel)))).ForEach(d =>
|
|
{
|
|
LogLevel lvl = (LogLevel)((int)d.Value);
|
|
ToolStripMenuItem item = new ToolStripMenuItem(d.Desc.ToString());
|
|
item.CheckOnClick = true;
|
|
item.Checked = true;
|
|
item.Tag = lvl;
|
|
item.CheckedChanged += LevelItem_CheckedChanged;
|
|
|
|
item.BackColor = lvl.GetEnumSelectedColor();
|
|
item.ForeColor = lvl.GetEnumSelectedFontColor();
|
|
tsmiLogLevels.DropDownItems.Add(item);
|
|
_showLevels.Add(lvl);
|
|
});
|
|
};
|
|
}
|
|
|
|
//public override void OnProcessUpdated()
|
|
//{
|
|
|
|
|
|
// Invoke(new Action(() =>
|
|
// {
|
|
// _showDevice.Clear();
|
|
// tsmiLogSources.DropDownItems.Clear();
|
|
// ToolStripMenuItem processItem = new ToolStripMenuItem(SOURCE_PROCESS);
|
|
// processItem.CheckOnClick = true;
|
|
// processItem.Checked = true;
|
|
// processItem.CheckedChanged += SourceItem_CheckedChanged;
|
|
// tsmiLogSources.DropDownItems.Add(processItem);
|
|
// _showDevice.Add(SOURCE_PROCESS);
|
|
|
|
// Process.DeviceCollection.ForEach(d =>
|
|
// {
|
|
// ToolStripMenuItem item = new ToolStripMenuItem(d.Name);
|
|
// item.CheckOnClick = true;
|
|
// item.Checked = true;
|
|
// item.CheckedChanged += SourceItem_CheckedChanged;
|
|
// tsmiLogSources.DropDownItems.Add(item);
|
|
// _showDevice.Add(d.Name);
|
|
// });
|
|
// }));
|
|
//}
|
|
|
|
private void LevelItem_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
_showLevels.Clear();
|
|
foreach (ToolStripMenuItem item in tsmiLogLevels.DropDownItems)
|
|
{
|
|
if (item.Checked)
|
|
{
|
|
LogLevel lv = (LogLevel)Convert.ToInt32(item.Tag);
|
|
_showLevels.Add(lv);
|
|
}
|
|
}
|
|
|
|
RefreshLogs();
|
|
}
|
|
|
|
private void SourceItem_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
_showDevice.Clear();
|
|
foreach (ToolStripMenuItem item in tsmiLogSources.DropDownItems)
|
|
{
|
|
if (item.Checked)
|
|
{
|
|
_showDevice.Add(item.Text);
|
|
}
|
|
}
|
|
RefreshLogs();
|
|
}
|
|
//public TaskFactory _taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.LongRunning);
|
|
readonly ConcurrentQueue<LogMsg> _logQueue = new ConcurrentQueue<LogMsg>();
|
|
Task _logTask = null;
|
|
static readonly object _logLock = new object();
|
|
|
|
List<LogMsg> _logBuffer = new List<LogMsg>();
|
|
List<LogLevel> _showLevels = new List<LogLevel>();
|
|
List<string> _showDevice = new List<string>();
|
|
const string SOURCE_PROCESS = "流程";
|
|
const int LOG_NUM_LIMIT = 20;
|
|
|
|
public void LogDisplay(LogMsg msg)
|
|
{
|
|
_logQueue.Enqueue(msg);
|
|
|
|
lock (_logLock)
|
|
{
|
|
if (_logTask == null)
|
|
{
|
|
_logTask = Task.Run(async () =>
|
|
{
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
Invoke(new Action(() =>
|
|
{
|
|
bool isNeedScroll = false;
|
|
while (_logQueue.TryDequeue(out LogMsg log))
|
|
{
|
|
_logBuffer.Add(log);
|
|
|
|
if (_showLevels.Contains(log.LogLevel) && (_showDevice.Count == 0 || (string.IsNullOrWhiteSpace(log.MsgSource) && _showDevice.Contains(SOURCE_PROCESS)) || _showDevice.Contains(log.MsgSource)))
|
|
{
|
|
isNeedScroll = true;
|
|
|
|
ListViewItem item = new ListViewItem($"{log.LogTime.ToString("HH:mm:ss.fff")}");
|
|
item.SubItems.Add($"{log.MsgSource}[{log.ThreadId}]");
|
|
item.SubItems.Add(log.Msg);
|
|
|
|
item.ToolTipText = log.Msg;
|
|
item.ForeColor = log.LogLevel.GetEnumSelectedFontColor();
|
|
item.BackColor = log.LogLevel.GetEnumSelectedColor();
|
|
|
|
lvLog.Items.Add(item);
|
|
}
|
|
}
|
|
|
|
if (_logBuffer.Count > LOG_NUM_LIMIT * 2)
|
|
{
|
|
_logBuffer = _logBuffer.Skip(_logBuffer.Count - LOG_NUM_LIMIT).ToList();
|
|
RefreshLogs();
|
|
isNeedScroll = true;
|
|
}
|
|
|
|
if (isNeedScroll && lvLog.Items.Count > 0)
|
|
{
|
|
RefreshLvLayout();
|
|
}
|
|
}));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
}
|
|
|
|
await Task.Delay(2000);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
private void RefreshLogs()
|
|
{
|
|
lvLog.Items.Clear();
|
|
|
|
_logBuffer.ForEach(log =>
|
|
{
|
|
if (_showLevels.Contains(log.LogLevel) && ((string.IsNullOrWhiteSpace(log.MsgSource) && _showDevice.Contains(SOURCE_PROCESS)) || _showDevice.Contains(log.MsgSource)))
|
|
{
|
|
ListViewItem item = new ListViewItem($"{log.LogTime.ToString("HH:mm:ss.fff")}");
|
|
item.SubItems.Add($"{log.MsgSource}[{log.ThreadId}]");
|
|
item.SubItems.Add(log.Msg);
|
|
|
|
item.ToolTipText = log.Msg;
|
|
item.ForeColor = log.LogLevel.GetEnumSelectedFontColor();
|
|
item.BackColor = log.LogLevel.GetEnumSelectedColor();
|
|
|
|
lvLog.Items.Add(item);
|
|
}
|
|
});
|
|
|
|
RefreshLvLayout();
|
|
}
|
|
|
|
private void lvLog_SizeChanged(object sender, EventArgs e)
|
|
{
|
|
RefreshLvLayout();
|
|
}
|
|
|
|
int width_1stCol = 80;
|
|
|
|
public event Action<LogMsg> OnLogMsgOutput;
|
|
|
|
private void RefreshLvLayout()
|
|
{
|
|
if (lvLog.Columns.Count <= 0)
|
|
return;
|
|
|
|
lvLog.Columns[0].Width = width_1stCol;
|
|
if (lvLog.Width <= lvLog.Height)
|
|
{
|
|
lvLog.Columns[1].Width = 0;
|
|
}
|
|
else
|
|
{
|
|
lvLog.Columns[1].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
|
}
|
|
lvLog.Columns[2].Width = lvLog.Width - width_1stCol - lvLog.Columns[1].Width - 10;
|
|
|
|
if (lvLog.Items.Count > 0)
|
|
lvLog.EnsureVisible(lvLog.Items.Count - 1);
|
|
}
|
|
|
|
private void tsmiClearLog_Click(object sender, EventArgs e)
|
|
{
|
|
lvLog.Items.Clear();
|
|
}
|
|
|
|
private void tsmiClearLog2_Click(object sender, EventArgs e)
|
|
{
|
|
lvLog.Items.Clear();
|
|
}
|
|
}
|
|
}
|