forked from PGR/ascnet
1
0
Fork 0
ascnet/AscNet.Logging/ThreadSafeStreamWriter.cs

61 lines
1.4 KiB
C#
Raw Normal View History

namespace AscNet.Logging
{
public class ThreadSafeStreamWriter : IDisposable
{
private static ThreadSafeStreamWriter? _instance;
private static StreamWriter _writer;
private bool disposedValue;
// TODO: add support for configuring
private string _logFilePath = "log.log";
private object _lock = new object();
2023-10-14 17:43:25 +00:00
private ThreadSafeStreamWriter()
{
_writer = new StreamWriter(_logFilePath, true);
#if !RELEASE
_writer.AutoFlush = true;
#endif
}
public static ThreadSafeStreamWriter Instance
{
get
{
return _instance ??= new ThreadSafeStreamWriter();
}
}
public void AppendLine(string line)
{
lock (_lock)
{
_writer.WriteLine(line);
}
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
_writer.Flush();
_writer.Close();
_writer.Dispose();
}
disposedValue = true;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}