Tranga-Website/Logging/MemoryLogger.cs

58 lines
1.5 KiB
C#
Raw Normal View History

2023-05-20 21:47:54 +02:00
using System.Text;
namespace Logging;
public class MemoryLogger : LoggerBase
{
private readonly SortedList<DateTime, LogMessage> _logMessages = new();
private int _lastLogMessageIndex = 0;
2023-05-20 21:47:54 +02:00
public MemoryLogger(Encoding? encoding = null) : base(encoding)
2023-05-20 21:47:54 +02:00
{
}
protected override void Write(LogMessage value)
{
2023-06-20 23:15:22 +02:00
while(!_logMessages.TryAdd(value.logTime, value))
Thread.Sleep(10);
2023-05-20 21:47:54 +02:00
}
public string[] GetLogMessage()
{
return Tail(Convert.ToUInt32(_logMessages.Count));
2023-05-20 21:47:54 +02:00
}
public string[] Tail(uint? length)
2023-05-20 21:47:54 +02:00
{
int retLength;
if (length is null || length > _logMessages.Count)
retLength = _logMessages.Count;
else
retLength = (int)length;
string[] ret = new string[retLength];
2023-05-20 21:47:54 +02:00
for (int retIndex = 0; retIndex < ret.Length; retIndex++)
{
ret[retIndex] = _logMessages.GetValueAtIndex(_logMessages.Count - retLength + retIndex).ToString();
}
_lastLogMessageIndex = _logMessages.Count - 1;
return ret;
}
public string[] GetNewLines()
{
int logMessageCount = _logMessages.Count;
string[] ret = new string[logMessageCount - _lastLogMessageIndex];
for (int retIndex = 0; retIndex < ret.Length; retIndex++)
{
ret[retIndex] = _logMessages.GetValueAtIndex(_lastLogMessageIndex + retIndex).ToString();
}
_lastLogMessageIndex = logMessageCount;
return ret;
2023-05-20 21:47:54 +02:00
}
}