feat: added loggers to event classes

This commit is contained in:
Syntriax 2025-07-12 16:53:01 +03:00
parent c8bb991865
commit 0e5cc8f898

View File

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using Syntriax.Engine.Core.Debug;
namespace Syntriax.Engine.Core;
/// <summary>
@ -48,6 +50,9 @@ public class Event
// We use Ascending order because draw calls are running from last to first
private static readonly Comparer<ListenerData> SortByAscendingPriority = Comparer<ListenerData>.Create((x, y) => x.Priority.CompareTo(y.Priority));
private ILogger _logger = new ConsoleLogger();
public ILogger Logger { get => _logger; set => _logger = value ?? new ConsoleLogger(); }
private readonly List<ListenerData> listeners = null!;
private readonly List<ListenerData> onceListeners = null!;
@ -126,7 +131,8 @@ public class Event
catch (Exception exception)
{
string methodCallRepresentation = $"{listeners[i].Callback.Method.DeclaringType?.FullName}.{listeners[i].Callback.Method.Name}()";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
Logger.LogError(this, $"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
Logger.LogException(this, exception);
}
for (int i = onceListeners.Count - 1; i >= 0; i--)
@ -135,7 +141,8 @@ public class Event
catch (Exception exception)
{
string methodCallRepresentation = $"{onceListeners[i].Callback.Method.DeclaringType?.FullName}.{onceListeners[i].Callback.Method.Name}()";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
Logger.LogError(this, $"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
Logger.LogException(this, exception);
}
onceListeners.RemoveAt(i);
}
@ -204,6 +211,9 @@ public class Event<TSender>
// We use Ascending order because draw calls are running from last to first
private static readonly Comparer<ListenerData> SortByAscendingPriority = Comparer<ListenerData>.Create((x, y) => x.Priority.CompareTo(y.Priority));
private ILogger _logger = new ConsoleLogger();
public ILogger Logger { get => _logger; set => _logger = value ?? new ConsoleLogger(); }
private readonly List<ListenerData> listeners = null!;
private readonly List<ListenerData> onceListeners = null!;
@ -283,7 +293,8 @@ public class Event<TSender>
catch (Exception exception)
{
string methodCallRepresentation = $"{listeners[i].Callback.Method.DeclaringType?.FullName}.{listeners[i].Callback.Method.Name}({sender})";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
Logger.LogError(sender, $"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
Logger.LogException(sender, exception);
}
for (int i = onceListeners.Count - 1; i >= 0; i--)
@ -292,7 +303,8 @@ public class Event<TSender>
catch (Exception exception)
{
string methodCallRepresentation = $"{onceListeners[i].Callback.Method.DeclaringType?.FullName}.{onceListeners[i].Callback.Method.Name}({sender})";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
Logger.LogError(sender, $"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
Logger.LogException(sender, exception);
}
onceListeners.RemoveAt(i);
}
@ -368,6 +380,9 @@ public class Event<TSender, TArguments>
// We use Ascending order because draw calls are running from last to first
private static readonly Comparer<ListenerData> SortByAscendingPriority = Comparer<ListenerData>.Create((x, y) => x.Priority.CompareTo(y.Priority));
private ILogger _logger = new ConsoleLogger();
public ILogger Logger { get => _logger; set => _logger = value ?? new ConsoleLogger(); }
private readonly List<ListenerData> listeners = null!;
private readonly List<ListenerData> onceListeners = null!;
@ -448,7 +463,8 @@ public class Event<TSender, TArguments>
catch (Exception exception)
{
string methodCallRepresentation = $"{listeners[i].Callback.Method.DeclaringType?.FullName}.{listeners[i].Callback.Method.Name}({string.Join(", ", sender, args)})";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
Logger.LogError(sender, $"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
Logger.LogException(sender, exception);
}
for (int i = onceListeners.Count - 1; i >= 0; i--)
@ -457,7 +473,8 @@ public class Event<TSender, TArguments>
catch (Exception exception)
{
string methodCallRepresentation = $"{onceListeners[i].Callback.Method.DeclaringType?.FullName}.{onceListeners[i].Callback.Method.Name}({string.Join(", ", sender, args)})";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
Logger.LogError(sender, $"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
Logger.LogException(sender, exception);
}
onceListeners.RemoveAt(i);
}