This commit is contained in:
zhishinet/xuyangyang
2026-04-19 17:33:11 +08:00
parent 88ef7dd3e1
commit 7c9351c2b5
4 changed files with 68 additions and 11 deletions

View File

@@ -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);
}
}
/// <summary>
/// 写入日志到TXT文件
/// </summary>
/// <param name="message">日志内容</param>
/// <param name="level">日志级别 (Error, Warn, Info)</param>
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");
}

View File

@@ -1557,7 +1557,7 @@ namespace 全自动水压检测仪
this.checkBox4.Enabled = false; this.checkBox4.Enabled = false;
this.checkBox4.Location = new System.Drawing.Point(425, 175); this.checkBox4.Location = new System.Drawing.Point(425, 175);
this.checkBox4.Name = "checkBox4"; 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.TabIndex = 30;
this.checkBox4.Text = "常温高液位"; this.checkBox4.Text = "常温高液位";
this.checkBox4.UseVisualStyleBackColor = true; this.checkBox4.UseVisualStyleBackColor = true;
@@ -1569,7 +1569,7 @@ namespace 全自动水压检测仪
this.checkBox3.Enabled = false; this.checkBox3.Enabled = false;
this.checkBox3.Location = new System.Drawing.Point(298, 175); this.checkBox3.Location = new System.Drawing.Point(298, 175);
this.checkBox3.Name = "checkBox3"; 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.TabIndex = 29;
this.checkBox3.Text = "常温低液位"; this.checkBox3.Text = "常温低液位";
this.checkBox3.UseVisualStyleBackColor = true; this.checkBox3.UseVisualStyleBackColor = true;
@@ -1581,7 +1581,7 @@ namespace 全自动水压检测仪
this.checkBox2.Enabled = false; this.checkBox2.Enabled = false;
this.checkBox2.Location = new System.Drawing.Point(150, 175); this.checkBox2.Location = new System.Drawing.Point(150, 175);
this.checkBox2.Name = "checkBox2"; 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.TabIndex = 28;
this.checkBox2.Text = "高温高液位"; this.checkBox2.Text = "高温高液位";
this.checkBox2.UseVisualStyleBackColor = true; this.checkBox2.UseVisualStyleBackColor = true;
@@ -1593,7 +1593,7 @@ namespace 全自动水压检测仪
this.checkBox1.Enabled = false; this.checkBox1.Enabled = false;
this.checkBox1.Location = new System.Drawing.Point(11, 175); this.checkBox1.Location = new System.Drawing.Point(11, 175);
this.checkBox1.Name = "checkBox1"; 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.TabIndex = 27;
this.checkBox1.Text = "高温低液位"; this.checkBox1.Text = "高温低液位";
this.checkBox1.UseVisualStyleBackColor = true; this.checkBox1.UseVisualStyleBackColor = true;

View File

@@ -321,7 +321,7 @@ namespace 全自动水压检测仪
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.WriteLine($"报警监控异常: {ex.Message}"); LogHelper.Error($"[NormalTemperatureMode - CheckAlarmStatusAsync] 报警监控异常: {ex.Message}{Environment.NewLine}{ex.StackTrace}");
} }
}; };
return timer; return timer;
@@ -2129,12 +2129,9 @@ namespace 全自动水压检测仪
}); });
} }
catch (Exception ex) catch (Exception uiEx)
{ {
SafeInvoke(() => LogHelper.Error($"[uiButton8_Click] : {uiEx.Message}{Environment.NewLine}{uiEx.StackTrace}");
{
MessageBox.Show($"操作失败:{ex.Message}");
});
} }
} }

View File

@@ -186,6 +186,7 @@
<Compile Include="Forms\UserManagerForm.Designer.cs"> <Compile Include="Forms\UserManagerForm.Designer.cs">
<DependentUpon>UserManagerForm.cs</DependentUpon> <DependentUpon>UserManagerForm.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="LogHelper.cs" />
<Compile Include="PasswordExpirationService.cs" /> <Compile Include="PasswordExpirationService.cs" />
<Compile Include="ScanImport.cs"> <Compile Include="ScanImport.cs">
<SubType>Form</SubType> <SubType>Form</SubType>
@@ -293,4 +294,4 @@
</PropertyGroup> </PropertyGroup>
<Error Condition="!Exists('..\packages\Microsoft.NETFramework.ReferenceAssemblies.net472.1.0.2\build\Microsoft.NETFramework.ReferenceAssemblies.net472.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.NETFramework.ReferenceAssemblies.net472.1.0.2\build\Microsoft.NETFramework.ReferenceAssemblies.net472.targets'))" /> <Error Condition="!Exists('..\packages\Microsoft.NETFramework.ReferenceAssemblies.net472.1.0.2\build\Microsoft.NETFramework.ReferenceAssemblies.net472.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.NETFramework.ReferenceAssemblies.net472.1.0.2\build\Microsoft.NETFramework.ReferenceAssemblies.net472.targets'))" />
</Target> </Target>
</Project> </Project>