2023-05-20 21:47:54 +02:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Logging;
|
|
|
|
|
|
|
|
|
|
public class MemoryLogger : LoggerBase
|
|
|
|
|
{
|
|
|
|
|
private SortedList<DateTime, LogMessage> logMessages = new();
|
|
|
|
|
|
|
|
|
|
public MemoryLogger(TextWriter? stdOut, Encoding? encoding = null) : base(stdOut, encoding)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Write(LogMessage value)
|
|
|
|
|
{
|
|
|
|
|
logMessages.Add(value.logTime, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string[] GetLogMessage()
|
|
|
|
|
{
|
2023-05-21 02:11:47 +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;
|
|
|
|
|
if (length is null || length > logMessages.Count)
|
|
|
|
|
retLength = logMessages.Count;
|
|
|
|
|
else
|
|
|
|
|
retLength = (int)length;
|
|
|
|
|
|
|
|
|
|
string[] ret = new string[retLength];
|
|
|
|
|
|
|
|
|
|
for (int logMessageIndex = logMessages.Count - retLength; logMessageIndex < logMessages.Count; logMessageIndex++)
|
|
|
|
|
ret[logMessageIndex + retLength - logMessages.Count] = logMessages.GetValueAtIndex(logMessageIndex).ToString();
|
2023-05-20 21:47:54 +02:00
|
|
|
|
|
2023-05-21 02:11:47 +02:00
|
|
|
|
return ret;
|
2023-05-20 21:47:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|