From 14513521257357ad50633290c1794b1340d06039 Mon Sep 17 00:00:00 2001 From: "liu.wenjie" Date: Wed, 17 Mar 2021 16:05:33 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=BC=98=E5=8C=96=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E6=9E=B6=E6=9E=84=EF=BC=8C=E5=87=8F=E5=B0=91=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E4=B9=8B=E9=97=B4=E7=9A=84=E5=BC=95=E7=94=A8=EF=BC=8C=E5=B0=86?= =?UTF-8?q?=E6=89=80=E6=9C=89=E5=B7=A5=E5=85=B7=E7=B1=BB=E5=BD=92=E5=B9=B6?= =?UTF-8?q?=E5=88=B0=E4=B8=80=E8=B5=B7=EF=BC=8C=E5=AF=B9=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E5=92=8C=E4=B8=BB=E5=87=BD=E6=95=B0=E4=B9=8B=E9=97=B4=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E8=A7=A3=E8=80=A6=202=E3=80=81=E5=A2=9E=E5=8A=A0Proje?= =?UTF-8?q?ct=E6=A6=82=E5=BF=B5=EF=BC=8C=E5=B9=B6=E5=AF=B9=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E8=BF=9B=E8=A1=8C=E4=BF=9D=E5=AD=98=E5=92=8C=E8=BD=BD?= =?UTF-8?q?=E5=85=A5=E3=80=82=203=E3=80=81=E4=BC=98=E5=8C=96LOG=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=EF=BC=8C=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9B=E5=85=B6?= =?UTF-8?q?=E4=BB=96=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ToolLib.Log/LogForm/DetailMesForm.Designer.cs | 137 +++++++++++++++ ToolLib.Log/LogForm/DetailMesForm.cs | 44 +++++ ToolLib.Log/LogForm/DetailMesForm.resx | 120 +++++++++++++ ToolLib.Log/LogForm/LogForm.csproj | 86 ++++++++++ .../LogForm/Properties/AssemblyInfo.cs | 36 ++++ ToolLib.Log/LogForm/UserLogger.Designer.cs | 91 ++++++++++ ToolLib.Log/LogForm/UserLogger.cs | 160 ++++++++++++++++++ ToolLib.Log/LogForm/UserLogger.resx | 123 ++++++++++++++ ToolLib.Log/Logger/Logger.csproj | 71 ++++++++ ToolLib.Log/Logger/LoggerClass.cs | 97 +++++++++++ ToolLib.Log/Logger/Properties/AssemblyInfo.cs | 36 ++++ ToolLib.Log/Logger/log4net.config | 54 ++++++ 12 files changed, 1055 insertions(+) create mode 100644 ToolLib.Log/LogForm/DetailMesForm.Designer.cs create mode 100644 ToolLib.Log/LogForm/DetailMesForm.cs create mode 100644 ToolLib.Log/LogForm/DetailMesForm.resx create mode 100644 ToolLib.Log/LogForm/LogForm.csproj create mode 100644 ToolLib.Log/LogForm/Properties/AssemblyInfo.cs create mode 100644 ToolLib.Log/LogForm/UserLogger.Designer.cs create mode 100644 ToolLib.Log/LogForm/UserLogger.cs create mode 100644 ToolLib.Log/LogForm/UserLogger.resx create mode 100644 ToolLib.Log/Logger/Logger.csproj create mode 100644 ToolLib.Log/Logger/LoggerClass.cs create mode 100644 ToolLib.Log/Logger/Properties/AssemblyInfo.cs create mode 100644 ToolLib.Log/Logger/log4net.config diff --git a/ToolLib.Log/LogForm/DetailMesForm.Designer.cs b/ToolLib.Log/LogForm/DetailMesForm.Designer.cs new file mode 100644 index 0000000..43b401d --- /dev/null +++ b/ToolLib.Log/LogForm/DetailMesForm.Designer.cs @@ -0,0 +1,137 @@ +namespace LogForm +{ + partial class DetailMesForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.btnClose = new System.Windows.Forms.Button(); + this.Rtb_DetailMes = new System.Windows.Forms.RichTextBox(); + this.lb_Level = new System.Windows.Forms.Label(); + this.lb_Time = new System.Windows.Forms.Label(); + this.lb_Detail = new System.Windows.Forms.Label(); + this.label3 = new System.Windows.Forms.Label(); + this.label1 = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // btnClose + // + this.btnClose.Location = new System.Drawing.Point(427, 334); + this.btnClose.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.btnClose.Name = "btnClose"; + this.btnClose.Size = new System.Drawing.Size(99, 33); + this.btnClose.TabIndex = 9; + this.btnClose.Text = "关闭"; + this.btnClose.UseVisualStyleBackColor = true; + this.btnClose.Click += new System.EventHandler(this.btnClose_Click); + // + // Rtb_DetailMes + // + this.Rtb_DetailMes.Location = new System.Drawing.Point(29, 140); + this.Rtb_DetailMes.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.Rtb_DetailMes.Name = "Rtb_DetailMes"; + this.Rtb_DetailMes.Size = new System.Drawing.Size(503, 182); + this.Rtb_DetailMes.TabIndex = 8; + this.Rtb_DetailMes.Text = ""; + // + // lb_Level + // + this.lb_Level.AutoSize = true; + this.lb_Level.Location = new System.Drawing.Point(110, 57); + this.lb_Level.Name = "lb_Level"; + this.lb_Level.Size = new System.Drawing.Size(0, 17); + this.lb_Level.TabIndex = 3; + // + // lb_Time + // + this.lb_Time.AutoSize = true; + this.lb_Time.Location = new System.Drawing.Point(110, 16); + this.lb_Time.Name = "lb_Time"; + this.lb_Time.Size = new System.Drawing.Size(0, 17); + this.lb_Time.TabIndex = 4; + // + // lb_Detail + // + this.lb_Detail.AutoSize = true; + this.lb_Detail.Location = new System.Drawing.Point(25, 100); + this.lb_Detail.Name = "lb_Detail"; + this.lb_Detail.Size = new System.Drawing.Size(79, 17); + this.lb_Detail.TabIndex = 5; + this.lb_Detail.Text = "Log详细信息"; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(25, 57); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(67, 17); + this.label3.TabIndex = 6; + this.label3.Text = "Log级别:"; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(25, 16); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(44, 17); + this.label1.TabIndex = 7; + this.label1.Text = "时间:"; + // + // DetailMesForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 17F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.BackColor = System.Drawing.Color.White; + this.ClientSize = new System.Drawing.Size(544, 381); + this.ControlBox = false; + this.Controls.Add(this.btnClose); + this.Controls.Add(this.Rtb_DetailMes); + this.Controls.Add(this.lb_Level); + this.Controls.Add(this.lb_Time); + this.Controls.Add(this.lb_Detail); + this.Controls.Add(this.label3); + this.Controls.Add(this.label1); + this.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); + this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.Name = "DetailMesForm"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; + this.Load += new System.EventHandler(this.DetailMesForm_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Button btnClose; + private System.Windows.Forms.RichTextBox Rtb_DetailMes; + private System.Windows.Forms.Label lb_Level; + private System.Windows.Forms.Label lb_Time; + private System.Windows.Forms.Label lb_Detail; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.Label label1; + } +} \ No newline at end of file diff --git a/ToolLib.Log/LogForm/DetailMesForm.cs b/ToolLib.Log/LogForm/DetailMesForm.cs new file mode 100644 index 0000000..76c108b --- /dev/null +++ b/ToolLib.Log/LogForm/DetailMesForm.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace LogForm +{ + public partial class DetailMesForm : Form + { + private string detailMsg = string.Empty; + public DetailMesForm(string detailMsg) + { + InitializeComponent(); + this.detailMsg = detailMsg; + } + + private void btnClose_Click(object sender, EventArgs e) + { + this.Close(); + } + + private void DetailMesForm_Load(object sender, EventArgs e) + { + try + { + string time = detailMsg.Split('>')[0]; + string level = detailMsg.Split(',')[0].Split('>')[1]; + lb_Time.Text = time; + lb_Level.Text = level; + Rtb_DetailMes.Text = detailMsg; + } + catch (Exception) + { + Rtb_DetailMes.Text = detailMsg; + } + + } + } +} diff --git a/ToolLib.Log/LogForm/DetailMesForm.resx b/ToolLib.Log/LogForm/DetailMesForm.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/ToolLib.Log/LogForm/DetailMesForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ToolLib.Log/LogForm/LogForm.csproj b/ToolLib.Log/LogForm/LogForm.csproj new file mode 100644 index 0000000..67ab386 --- /dev/null +++ b/ToolLib.Log/LogForm/LogForm.csproj @@ -0,0 +1,86 @@ + + + + + Debug + AnyCPU + {6B38D68F-E77B-4761-BDE5-A261EA8925DD} + Library + Properties + LogForm + LogForm + v4.5.2 + 512 + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + Form + + + DetailMesForm.cs + + + + UserControl + + + UserLogger.cs + + + + + DetailMesForm.cs + + + UserLogger.cs + + + + + {bec2389f-b8b4-4e8a-8c71-189cde14e962} + EnumList + + + {d4e052b9-e541-4b67-a1f9-273073ef1d4b} + Logger + + + + + \ No newline at end of file diff --git a/ToolLib.Log/LogForm/Properties/AssemblyInfo.cs b/ToolLib.Log/LogForm/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..6618900 --- /dev/null +++ b/ToolLib.Log/LogForm/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("LogForm")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("LogForm")] +[assembly: AssemblyCopyright("Copyright © 2020")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +//将 ComVisible 设置为 false 将使此程序集中的类型 +//对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("6b38d68f-e77b-4761-bde5-a261ea8925dd")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, +// 方法是按如下所示使用“*”: : +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ToolLib.Log/LogForm/UserLogger.Designer.cs b/ToolLib.Log/LogForm/UserLogger.Designer.cs new file mode 100644 index 0000000..45bb0b7 --- /dev/null +++ b/ToolLib.Log/LogForm/UserLogger.Designer.cs @@ -0,0 +1,91 @@ +namespace LogForm +{ + partial class UserLogger + { + /// + /// 必需的设计器变量。 + /// + private System.ComponentModel.IContainer components = null; + + /// + /// 清理所有正在使用的资源。 + /// + /// 如果应释放托管资源,为 true;否则为 false。 + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region 组件设计器生成的代码 + + /// + /// 设计器支持所需的方法 - 不要修改 + /// 使用代码编辑器修改此方法的内容。 + /// + private void InitializeComponent() + { + this.components = new System.ComponentModel.Container(); + this.listBox1 = new System.Windows.Forms.ListBox(); + this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); + this.ClearLog = new System.Windows.Forms.ToolStripMenuItem(); + this.contextMenuStrip1.SuspendLayout(); + this.SuspendLayout(); + // + // listBox1 + // + this.listBox1.BackColor = System.Drawing.SystemColors.Info; + this.listBox1.Dock = System.Windows.Forms.DockStyle.Fill; + this.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; + this.listBox1.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); + this.listBox1.FormattingEnabled = true; + this.listBox1.HorizontalScrollbar = true; + this.listBox1.ItemHeight = 18; + this.listBox1.Location = new System.Drawing.Point(0, 0); + this.listBox1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.listBox1.Name = "listBox1"; + this.listBox1.Size = new System.Drawing.Size(365, 266); + this.listBox1.TabIndex = 0; + this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem); + this.listBox1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseDoubleClick); + // + // contextMenuStrip1 + // + this.contextMenuStrip1.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); + this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.ClearLog}); + this.contextMenuStrip1.Name = "contextMenuStrip1"; + this.contextMenuStrip1.Size = new System.Drawing.Size(107, 28); + // + // ClearLog + // + this.ClearLog.Name = "ClearLog"; + this.ClearLog.Size = new System.Drawing.Size(106, 24); + this.ClearLog.Text = "清空"; + this.ClearLog.Click += new System.EventHandler(this.ClearLog_Click); + // + // UserLogger + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.AutoSize = true; + this.Controls.Add(this.listBox1); + this.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); + this.Name = "UserLogger"; + this.Size = new System.Drawing.Size(365, 266); + this.Load += new System.EventHandler(this.UserLogger_Load); + this.contextMenuStrip1.ResumeLayout(false); + this.ResumeLayout(false); + + } + + #endregion + + public System.Windows.Forms.ListBox listBox1; + private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; + private System.Windows.Forms.ToolStripMenuItem ClearLog; + } +} diff --git a/ToolLib.Log/LogForm/UserLogger.cs b/ToolLib.Log/LogForm/UserLogger.cs new file mode 100644 index 0000000..a75f1e9 --- /dev/null +++ b/ToolLib.Log/LogForm/UserLogger.cs @@ -0,0 +1,160 @@ +using System; +using System.Drawing; +using System.Windows.Forms; +using Logger; +using System.Threading.Tasks; +using System.IO; +using System.Collections.Generic; + +namespace LogForm +{ + public partial class UserLogger : UserControl + { + bool logFocus = true; + string logDictory = @"C:\MyCCDSystem\Log\"; + public UserLogger() + { + InitializeComponent(); + } + ~UserLogger() + { + logFocus = false; + } + + private void listBox1_DrawItem(object sender, DrawItemEventArgs e) + { + if (e.Index < 0) + { return; } + else + { + e.DrawBackground(); + Brush mybsh = Brushes.Black; + // 判断是什么类型的标签,在调用时必须信息前边标注信息的类别,分为Info,Warning,Error + try + { + MsgLevel msgLevel = (MsgLevel)Enum.Parse(typeof(MsgLevel), listBox1.Items[e.Index].ToString().Split('>')[1].Trim().Split(',')[0]); + switch (msgLevel) + { + case MsgLevel.Info: + mybsh = Brushes.Green; + break; + case MsgLevel.Debug: + mybsh = Brushes.Green; + break; + case MsgLevel.Warn: + mybsh = Brushes.Purple; + break; + case MsgLevel.Exception: + case MsgLevel.Fatal: + mybsh = Brushes.Red; + break; + default: + mybsh = Brushes.Black; + break; + } + } + catch (Exception) + { + mybsh = Brushes.Green; + } + e.DrawFocusRectangle(); + e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, mybsh, e.Bounds, StringFormat.GenericDefault); + } + } + + + public void AddLog(MsgLevel msgLog, string logInfo) + { + try + { + this.Invoke((MethodInvoker)delegate + { + string recordMsg = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} -> {msgLog.ToString()}, {logInfo}"; + listBox1.Items.Add(recordMsg); + WriteLog(recordMsg); + listBox1.SelectedIndex = listBox1.Items.Count - 1; + if (listBox1.Items.Count > 1000) + { + listBox1.Items.Clear(); + } + Application.DoEvents(); + }); + } + catch(Exception) + { + + } + } + public void AddLog(MsgLevel msgLog, string logInfo, Exception ex) + { + try + { + this.Invoke((MethodInvoker)delegate + { + listBox1.Items.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " -> " + msgLog.ToString() + "," + logInfo + ex); + listBox1.SelectedIndex = listBox1.Items.Count - 1; + WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " -> " + msgLog.ToString() + "," + logInfo + ex); + if(listBox1.Items.Count >1000) + { + listBox1.Items.Clear(); + } + Application.DoEvents(); + }); + } + catch (Exception) + { + + } + } + public void WriteLog(string msg) + { + string time = DateTime.Now.ToString("HH:mm:ss.fff"); + if (!Directory.Exists(logDictory)) + { + Directory.CreateDirectory(logDictory); + } + string runningLogFileName = logDictory + DateTime.Now.ToString("yyyyMMdd") + ".log"; + StreamWriter mySW = new StreamWriter(runningLogFileName, true); + mySW.WriteLine(msg); + mySW.Close(); + } + private void UserLogger_Load(object sender, EventArgs e) + { + Task startLogFocus = new Task(() => + { + while (logFocus) + { + if (LoggerClass.logQueue.Count > 0) + { + LogInfo log = LoggerClass.logQueue.Dequeue(); + if (log.ex != null) + { + AddLog(log.logLevel, log.message, log.ex); + } + else + { + AddLog(log.logLevel, log.message); + } + + } + } + }); + startLogFocus.Start(); + } + + private void ClearLog_Click(object sender, EventArgs e) + { + this.listBox1.Items.Clear(); + } + + private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e) + { + if(listBox1.SelectedIndex != -1) + { + string detalText = listBox1.SelectedItem.ToString(); + DetailMesForm myForm = new DetailMesForm(detalText); + myForm.Show(); + } + } + } +} diff --git a/ToolLib.Log/LogForm/UserLogger.resx b/ToolLib.Log/LogForm/UserLogger.resx new file mode 100644 index 0000000..ad53752 --- /dev/null +++ b/ToolLib.Log/LogForm/UserLogger.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/ToolLib.Log/Logger/Logger.csproj b/ToolLib.Log/Logger/Logger.csproj new file mode 100644 index 0000000..f560130 --- /dev/null +++ b/ToolLib.Log/Logger/Logger.csproj @@ -0,0 +1,71 @@ + + + + + Debug + AnyCPU + {D4E052B9-E541-4B67-A1F9-273073EF1D4B} + Library + Properties + Logger + Logger + v4.5.2 + 512 + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + False + F:\VSCode\LearnTest\Reflection\bin\Debug\log4net.dll + + + + + + + + + + + + + + + + + + PreserveNewest + + + + + {bec2389f-b8b4-4e8a-8c71-189cde14e962} + EnumList + + + + + \ No newline at end of file diff --git a/ToolLib.Log/Logger/LoggerClass.cs b/ToolLib.Log/Logger/LoggerClass.cs new file mode 100644 index 0000000..628ca68 --- /dev/null +++ b/ToolLib.Log/Logger/LoggerClass.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using log4net; +using System.Windows.Forms; + +namespace Logger +{ + public class LoggerClass + { + public static readonly ILog loginfo = LogManager.GetLogger("loginfo"); + public static readonly ILog logerror = LogManager.GetLogger("logerror"); + /// + /// Log队列 + /// + public static Queue logQueue { get; set; } = new Queue() { }; + public static void WriteLog(string info, bool ShowMsgBox = false) + { + if (loginfo.IsInfoEnabled) + { + loginfo.Info(info); + } + logQueue.Enqueue(new LogInfo{ message = info, ex = null, logLevel = MsgLevel.Info}); + if(ShowMsgBox) + { + MessageBox.Show(info); + } + } + public static void WriteLog(string info, MsgLevel msgLevel, bool ShowMsgBox = false) + { + if (loginfo.IsInfoEnabled) + { + loginfo.Info(info); + } + logQueue.Enqueue(new LogInfo { message = info,ex = null, logLevel = msgLevel }); + if (ShowMsgBox) + { + MessageBox.Show(info); + } + } + public static void WriteLog(string info, MsgLevel msgLevel, Exception ex, bool ShowMsgBox = false) + { + if (logerror.IsErrorEnabled) + { + logerror.Error(info, ex); + } + logQueue.Enqueue(new LogInfo { message = info, ex = ex, logLevel = msgLevel }); + if (ShowMsgBox) + { + MessageBox.Show(info); + } + } + public static void WriteLog(string info, Exception ex, bool ShowMsgBox = false) + { + if (logerror.IsErrorEnabled) + { + logerror.Error(info, ex); + } + logQueue.Enqueue(new LogInfo { message = info, ex = ex, logLevel = MsgLevel.Exception }); + if (ShowMsgBox) + { + MessageBox.Show(info); + } + } + } + public class LogInfo + { + public string message { get; set; } + public MsgLevel logLevel { get; set; } + public Exception ex { get; set; } + } + public enum MsgLevel + { + /// + /// 0.调试信息输出 + /// + Debug = 0, + /// + /// 1.业务信息记录 + /// + Info = 1, + /// + /// 2.警告提醒(捕获的业务异常) + /// + Warn = 2, + /// + /// 3.发生了异常(捕获的系统异常) + /// + Exception = 3, + /// + /// 4.发生致命异常(未被捕获的异常|捕获的业务逻辑异常) + /// + Fatal = 4 + } +} diff --git a/ToolLib.Log/Logger/Properties/AssemblyInfo.cs b/ToolLib.Log/Logger/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..07b7e71 --- /dev/null +++ b/ToolLib.Log/Logger/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("Logger")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Logger")] +[assembly: AssemblyCopyright("Copyright © 2020")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] +[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)] +//将 ComVisible 设置为 false 将使此程序集中的类型 +//对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("d4e052b9-e541-4b67-a1f9-273073ef1d4b")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, +// 方法是按如下所示使用“*”: : +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] +//[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)] diff --git a/ToolLib.Log/Logger/log4net.config b/ToolLib.Log/Logger/log4net.config new file mode 100644 index 0000000..1428ef1 --- /dev/null +++ b/ToolLib.Log/Logger/log4net.config @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file