增加日志

This commit is contained in:
2025-04-01 18:15:30 +08:00
parent e08386333a
commit 409089e2ca
21 changed files with 1838 additions and 354 deletions

148
DHSoftware/Views/FrmLog.Designer.cs generated Normal file
View File

@ -0,0 +1,148 @@
namespace DHSoftware.Views
{
partial class FrmLog
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.tsmiClearLog = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.tsmiLogLevels = new System.Windows.Forms.ToolStripMenuItem();
this.tsmiLogSources = new System.Windows.Forms.ToolStripMenuItem();
this.tsmiClearLog2 = new System.Windows.Forms.ToolStripMenuItem();
this.lvLog = new System.Windows.Forms.ListView();
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.contextMenuStrip1.SuspendLayout();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.tsmiClearLog});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(125, 26);
//
// tsmiClearLog
//
this.tsmiClearLog.Name = "tsmiClearLog";
this.tsmiClearLog.Size = new System.Drawing.Size(124, 22);
this.tsmiClearLog.Text = "清空日志";
this.tsmiClearLog.Click += new System.EventHandler(this.tsmiClearLog_Click);
//
// menuStrip1
//
this.menuStrip1.BackColor = System.Drawing.Color.Transparent;
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.tsmiLogLevels,
this.tsmiLogSources,
this.tsmiClearLog2});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(800, 25);
this.menuStrip1.TabIndex = 3;
this.menuStrip1.Text = "menuStrip1";
//
// tsmiLogLevels
//
this.tsmiLogLevels.Name = "tsmiLogLevels";
this.tsmiLogLevels.Size = new System.Drawing.Size(68, 21);
this.tsmiLogLevels.Text = "日志级别";
//
// tsmiLogSources
//
this.tsmiLogSources.Name = "tsmiLogSources";
this.tsmiLogSources.Size = new System.Drawing.Size(68, 21);
this.tsmiLogSources.Text = "日志来源";
//
// tsmiClearLog2
//
this.tsmiClearLog2.Name = "tsmiClearLog2";
this.tsmiClearLog2.Size = new System.Drawing.Size(68, 21);
this.tsmiClearLog2.Text = "清空日志";
this.tsmiClearLog2.Click += new System.EventHandler(this.tsmiClearLog2_Click);
//
// lvLog
//
this.lvLog.BackColor = System.Drawing.SystemColors.Control;
this.lvLog.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.lvLog.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2,
this.columnHeader3});
this.lvLog.ContextMenuStrip = this.contextMenuStrip1;
this.lvLog.Dock = System.Windows.Forms.DockStyle.Fill;
this.lvLog.FullRowSelect = true;
this.lvLog.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
this.lvLog.HideSelection = false;
this.lvLog.Location = new System.Drawing.Point(0, 25);
this.lvLog.MultiSelect = false;
this.lvLog.Name = "lvLog";
this.lvLog.Size = new System.Drawing.Size(800, 463);
this.lvLog.TabIndex = 5;
this.lvLog.UseCompatibleStateImageBehavior = false;
this.lvLog.View = System.Windows.Forms.View.Details;
this.lvLog.SizeChanged += new System.EventHandler(this.lvLog_SizeChanged);
//
// columnHeader1
//
this.columnHeader1.Width = 80;
//
// FrmLog
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
this.ClientSize = new System.Drawing.Size(800, 488);
this.Controls.Add(this.lvLog);
this.Controls.Add(this.menuStrip1);
//this.MainMenuStrip = this.menuStrip1;
this.Name = "FrmLog";
this.Text = "日志";
this.contextMenuStrip1.ResumeLayout(false);
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem tsmiClearLog;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem tsmiLogLevels;
private System.Windows.Forms.ToolStripMenuItem tsmiLogSources;
private System.Windows.Forms.ListView lvLog;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.Windows.Forms.ToolStripMenuItem tsmiClearLog2;
//private System.Windows.Forms.ToolStripMenuItem testToolStripMenuItem;
}
}

236
DHSoftware/Views/FrmLog.cs Normal file
View File

@ -0,0 +1,236 @@

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();
}
}
}

View File

@ -0,0 +1,126 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="contextMenuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>182, 17</value>
</metadata>
</root>