添加项目文件。

This commit is contained in:
liming 蔡
2026-07-14 13:55:17 +08:00
parent 63759495f2
commit 8bbdf78731
335 changed files with 81415 additions and 0 deletions
+73
View File
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace SimpleCommunication
{
public class LogHelper : IDisposable
{
private static LogHelper logHelper;
private static object singleLock = new object();
private string logPath = Path.Combine(System.Environment.CurrentDirectory, "Log.txt");
private string spaceSymbols = "\r\n\r\n--------------------------------------------\r\n\r\n";
private byte[] spaceSymbolsBytes;
private FileStream fileStream;
private LogHelper()
{
}
public static LogHelper GetInstance()
{
if (logHelper == null)
{
lock (singleLock)
{
if (logHelper == null)
{
logHelper = new LogHelper();
}
}
}
return logHelper;
}
public void SetLogPath(string logPath)
{
this.logPath = Path.Combine(logPath, "Log.txt");
}
public void Init()
{
fileStream = new FileStream(logPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
spaceSymbolsBytes = Encoding.Default.GetBytes(spaceSymbols);
}
public void SetSpaceSymbols(string spaceSymbols)
{
this.spaceSymbols = spaceSymbols;
this.spaceSymbolsBytes = Encoding.Default.GetBytes(spaceSymbols);
}
public void Log(string content)
{
if (!string.IsNullOrEmpty(content))
{
fileStream.Seek(0, SeekOrigin.End);
byte[] bytes = Encoding.Default.GetBytes(content);
fileStream.Write(bytes, 0, bytes.Length);
fileStream.Write(spaceSymbolsBytes, 0, spaceSymbolsBytes.Length);
fileStream.Flush();
}
}
public void Dispose()
{
if (fileStream != null)
{
fileStream.Dispose();
}
}
}
}