mirror of
https://git.lewd.wtf/PGR/ascnet
synced 2025-12-14 23:14:37 +01:00
add proper logger, saves to file as well
This commit is contained in:
60
AscNet.Logging/ThreadSafeStreamWriter.cs
Normal file
60
AscNet.Logging/ThreadSafeStreamWriter.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
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();
|
||||
|
||||
public 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user