diff --git a/全自动水压检测仪/LogHelper.cs b/全自动水压检测仪/LogHelper.cs new file mode 100644 index 0000000..c395525 --- /dev/null +++ b/全自动水压检测仪/LogHelper.cs @@ -0,0 +1,59 @@ +using System; +using System.IO; +using System.Threading; + +// 建议放在单独的文件或命名空间底部 +public static class LogHelper +{ + // 定义日志文件夹路径(例如:项目目录下的 Logs 文件夹) + private static readonly string LogDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs"); + + // 确保文件夹存在 + static LogHelper() + { + if (!Directory.Exists(LogDirectory)) + { + Directory.CreateDirectory(LogDirectory); + } + } + + /// + /// 写入日志到TXT文件 + /// + /// 日志内容 + /// 日志级别 (Error, Warn, Info) + public static void WriteLog(string message, string level = "Info") + { + try + { + // 生成文件名:Log_2023-10-05.txt + string fileName = $"Log_{DateTime.Now:yyyy-MM-dd}.txt"; + string filePath = Path.Combine(LogDirectory, fileName); + + // 构建日志行 + // [2023-10-05 14:30:25] [Error] 发生了xxx错误 + string logLine = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] [{level}] {message}{Environment.NewLine}"; + + // 使用 FileStream 以追加模式写入,支持多线程安全写入 + // 注意:这里使用了锁,防止多线程同时写入冲突 + lock (typeof(LogHelper)) + { + using (var stream = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)) + using (var writer = new StreamWriter(stream)) + { + writer.Write(logLine); + } + } + } + catch (Exception ex) + { + // 防止日志系统自身崩溃导致程序异常,这里只用 Debug 输出错误(或者什么都不做) + // Debug.WriteLine($"日志写入失败: {ex.Message}"); + } + } + + // 为了方便,可以封装几个常用方法 + public static void Error(string message) => WriteLog(message, "ERROR"); + public static void Warn(string message) => WriteLog(message, "WARN"); + public static void Info(string message) => WriteLog(message, "INFO"); +} \ No newline at end of file diff --git a/全自动水压检测仪/NormalTemperatureMode.Designer.cs b/全自动水压检测仪/NormalTemperatureMode.Designer.cs index 1331bb9..400b0fc 100644 --- a/全自动水压检测仪/NormalTemperatureMode.Designer.cs +++ b/全自动水压检测仪/NormalTemperatureMode.Designer.cs @@ -1557,7 +1557,7 @@ namespace 全自动水压检测仪 this.checkBox4.Enabled = false; this.checkBox4.Location = new System.Drawing.Point(425, 175); this.checkBox4.Name = "checkBox4"; - this.checkBox4.Size = new System.Drawing.Size(131, 24); + this.checkBox4.Size = new System.Drawing.Size(106, 20); this.checkBox4.TabIndex = 30; this.checkBox4.Text = "常温高液位"; this.checkBox4.UseVisualStyleBackColor = true; @@ -1569,7 +1569,7 @@ namespace 全自动水压检测仪 this.checkBox3.Enabled = false; this.checkBox3.Location = new System.Drawing.Point(298, 175); this.checkBox3.Name = "checkBox3"; - this.checkBox3.Size = new System.Drawing.Size(131, 24); + this.checkBox3.Size = new System.Drawing.Size(106, 20); this.checkBox3.TabIndex = 29; this.checkBox3.Text = "常温低液位"; this.checkBox3.UseVisualStyleBackColor = true; @@ -1581,7 +1581,7 @@ namespace 全自动水压检测仪 this.checkBox2.Enabled = false; this.checkBox2.Location = new System.Drawing.Point(150, 175); this.checkBox2.Name = "checkBox2"; - this.checkBox2.Size = new System.Drawing.Size(131, 24); + this.checkBox2.Size = new System.Drawing.Size(106, 20); this.checkBox2.TabIndex = 28; this.checkBox2.Text = "高温高液位"; this.checkBox2.UseVisualStyleBackColor = true; @@ -1593,7 +1593,7 @@ namespace 全自动水压检测仪 this.checkBox1.Enabled = false; this.checkBox1.Location = new System.Drawing.Point(11, 175); this.checkBox1.Name = "checkBox1"; - this.checkBox1.Size = new System.Drawing.Size(131, 24); + this.checkBox1.Size = new System.Drawing.Size(106, 20); this.checkBox1.TabIndex = 27; this.checkBox1.Text = "高温低液位"; this.checkBox1.UseVisualStyleBackColor = true; diff --git a/全自动水压检测仪/NormalTemperatureMode.cs b/全自动水压检测仪/NormalTemperatureMode.cs index 79321e9..d469687 100644 --- a/全自动水压检测仪/NormalTemperatureMode.cs +++ b/全自动水压检测仪/NormalTemperatureMode.cs @@ -321,7 +321,7 @@ namespace 全自动水压检测仪 } catch (Exception ex) { - Debug.WriteLine($"报警监控异常: {ex.Message}"); + LogHelper.Error($"[NormalTemperatureMode - CheckAlarmStatusAsync] 报警监控异常: {ex.Message}{Environment.NewLine}{ex.StackTrace}"); } }; return timer; @@ -2129,12 +2129,9 @@ namespace 全自动水压检测仪 }); } - catch (Exception ex) + catch (Exception uiEx) { - SafeInvoke(() => - { - MessageBox.Show($"操作失败:{ex.Message}"); - }); + LogHelper.Error($"[uiButton8_Click] : {uiEx.Message}{Environment.NewLine}{uiEx.StackTrace}"); } } diff --git a/全自动水压检测仪/全自动水压检测仪.csproj b/全自动水压检测仪/全自动水压检测仪.csproj index 6414721..d188695 100644 --- a/全自动水压检测仪/全自动水压检测仪.csproj +++ b/全自动水压检测仪/全自动水压检测仪.csproj @@ -186,6 +186,7 @@ UserManagerForm.cs + Form @@ -293,4 +294,4 @@ - + \ No newline at end of file