1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00
Diogo Trindade 1b62109961 Debug - Fixed threading issue with LogStore
also made the logger viewer text searching much more efficient
2023-03-01 17:35:35 +00:00

71 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Serilog.Events;
namespace Artemis.Core;
/// <summary>
/// A static store containing the last 500 logging events
/// </summary>
public static class LogStore
{
private static readonly object _lock = new();
private static readonly LinkedList<LogEvent> LinkedList = new();
/// <summary>
/// Gets a list containing the last 500 log events.
/// </summary>
public static List<LogEvent> Events
{
get
{
List<LogEvent> events;
lock (_lock)
events = LinkedList.ToList();
return events;
}
}
/// <summary>
/// Occurs when a new <see cref="LogEvent" /> was received.
/// </summary>
public static event EventHandler<LogEventEventArgs>? EventAdded;
internal static void Emit(LogEvent logEvent)
{
lock (_lock)
{
LinkedList.AddLast(logEvent);
while (LinkedList.Count > 500)
LinkedList.RemoveFirst();
}
OnEventAdded(new LogEventEventArgs(logEvent));
}
private static void OnEventAdded(LogEventEventArgs e)
{
EventAdded?.Invoke(null, e);
}
}
/// <summary>
/// Contains log event related data
/// </summary>
public class LogEventEventArgs : EventArgs
{
internal LogEventEventArgs(LogEvent logEvent)
{
LogEvent = logEvent;
}
/// <summary>
/// Gets the log event
/// </summary>
public LogEvent LogEvent { get; }
}