74 lines
2.1 KiB
C#
74 lines
2.1 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.IO;
|
|||
|
|
|
|||
|
|
namespace SimpleServer
|
|||
|
|
{
|
|||
|
|
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();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|