添加项目文件。
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user