2023-05-20 21:47:54 +02:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Logging;
|
|
|
|
|
|
|
|
|
|
public class MemoryLogger : LoggerBase
|
|
|
|
|
{
|
2023-05-21 14:44:33 +02:00
|
|
|
|
private readonly SortedList<DateTime, LogMessage> _logMessages = new();
|
|
|
|
|
private int _lastLogMessageIndex = 0;
|
2023-05-20 21:47:54 +02:00
|
|
|
|
|
2023-07-16 17:33:15 +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()
|
|
|
|
|
{
|
2023-05-21 14:44:33 +02:00
|
|
|
|
return Tail(Convert.ToUInt32(_logMessages.Count));
|
2023-05-20 21:47:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-21 02:11:47 +02:00
|
|
|
|
public string[] Tail(uint? length)
|
2023-05-20 21:47:54 +02:00
|
|
|
|
{
|
2023-05-21 02:11:47 +02:00
|
|
|
|
int retLength;
|
2023-05-21 14:44:33 +02:00
|
|
|
|
if (length is null || length > _logMessages.Count)
|
|
|
|
|
retLength = _logMessages.Count;
|
2023-05-21 02:11:47 +02:00
|
|
|
|
else
|
|
|
|
|
retLength = (int)length;
|
|
|
|
|
|
|
|
|
|
string[] ret = new string[retLength];
|
2023-05-20 21:47:54 +02:00
|
|
|
|
|
2023-05-21 14:44:33 +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;
|
2023-05-21 02:11:47 +02:00
|
|
|
|
return ret;
|
2023-05-20 21:47:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|