refactor: moved event log calls to a shared method

This commit is contained in:
Syntriax 2025-07-12 17:34:56 +03:00
parent 42064875a0
commit 7db56e7f3e

View File

@ -131,8 +131,7 @@ public class Event
catch (Exception exception) catch (Exception exception)
{ {
string methodCallRepresentation = $"{listeners[i].Callback.Method.DeclaringType?.FullName}.{listeners[i].Callback.Method.Name}()"; string methodCallRepresentation = $"{listeners[i].Callback.Method.DeclaringType?.FullName}.{listeners[i].Callback.Method.Name}()";
Logger.LogError(this, $"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}"); EventHelpers.LogInvocationException(listeners[i].Callback.Target ?? this, Logger, exception, methodCallRepresentation);
Logger.LogException(this, exception);
} }
for (int i = onceListeners.Count - 1; i >= 0; i--) for (int i = onceListeners.Count - 1; i >= 0; i--)
@ -141,8 +140,7 @@ public class Event
catch (Exception exception) catch (Exception exception)
{ {
string methodCallRepresentation = $"{onceListeners[i].Callback.Method.DeclaringType?.FullName}.{onceListeners[i].Callback.Method.Name}()"; string methodCallRepresentation = $"{onceListeners[i].Callback.Method.DeclaringType?.FullName}.{onceListeners[i].Callback.Method.Name}()";
Logger.LogError(this, $"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}"); EventHelpers.LogInvocationException(onceListeners[i].Callback.Target ?? this, Logger, exception, methodCallRepresentation);
Logger.LogException(this, exception);
} }
onceListeners.RemoveAt(i); onceListeners.RemoveAt(i);
} }
@ -293,8 +291,7 @@ public class Event<TSender> where TSender : class
catch (Exception exception) catch (Exception exception)
{ {
string methodCallRepresentation = $"{listeners[i].Callback.Method.DeclaringType?.FullName}.{listeners[i].Callback.Method.Name}({sender})"; string methodCallRepresentation = $"{listeners[i].Callback.Method.DeclaringType?.FullName}.{listeners[i].Callback.Method.Name}({sender})";
Logger.LogError(sender, $"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}"); EventHelpers.LogInvocationException(listeners[i].Callback.Target ?? sender, Logger, exception, methodCallRepresentation);
Logger.LogException(sender, exception);
} }
for (int i = onceListeners.Count - 1; i >= 0; i--) for (int i = onceListeners.Count - 1; i >= 0; i--)
@ -303,8 +300,7 @@ public class Event<TSender> where TSender : class
catch (Exception exception) catch (Exception exception)
{ {
string methodCallRepresentation = $"{onceListeners[i].Callback.Method.DeclaringType?.FullName}.{onceListeners[i].Callback.Method.Name}({sender})"; string methodCallRepresentation = $"{onceListeners[i].Callback.Method.DeclaringType?.FullName}.{onceListeners[i].Callback.Method.Name}({sender})";
Logger.LogError(sender, $"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}"); EventHelpers.LogInvocationException(onceListeners[i].Callback.Target ?? sender, Logger, exception, methodCallRepresentation);
Logger.LogException(sender, exception);
} }
onceListeners.RemoveAt(i); onceListeners.RemoveAt(i);
} }
@ -462,9 +458,8 @@ public class Event<TSender, TArguments> where TSender : class
try { listeners[i].Callback.Invoke(sender, args); } try { listeners[i].Callback.Invoke(sender, args); }
catch (Exception exception) catch (Exception exception)
{ {
string methodCallRepresentation = $"{listeners[i].Callback.Method.DeclaringType?.FullName}.{listeners[i].Callback.Method.Name}({string.Join(", ", sender, args)})"; string methodCallRepresentation = $"{listeners[i].Callback.Method.DeclaringType?.FullName}.{listeners[i].Callback.Method.Name}({sender}, {args})";
Logger.LogError(sender, $"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}"); EventHelpers.LogInvocationException(listeners[i].Callback.Target ?? sender, Logger, exception, methodCallRepresentation);
Logger.LogException(sender, exception);
} }
for (int i = onceListeners.Count - 1; i >= 0; i--) for (int i = onceListeners.Count - 1; i >= 0; i--)
@ -472,9 +467,8 @@ public class Event<TSender, TArguments> where TSender : class
try { onceListeners[i].Callback.Invoke(sender, args); } try { onceListeners[i].Callback.Invoke(sender, args); }
catch (Exception exception) catch (Exception exception)
{ {
string methodCallRepresentation = $"{onceListeners[i].Callback.Method.DeclaringType?.FullName}.{onceListeners[i].Callback.Method.Name}({string.Join(", ", sender, args)})"; string methodCallRepresentation = $"{onceListeners[i].Callback.Method.DeclaringType?.FullName}.{onceListeners[i].Callback.Method.Name}({sender}, {args})";
Logger.LogError(sender, $"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}"); EventHelpers.LogInvocationException(onceListeners[i].Callback.Target ?? sender, Logger, exception, methodCallRepresentation);
Logger.LogException(sender, exception);
} }
onceListeners.RemoveAt(i); onceListeners.RemoveAt(i);
} }
@ -495,3 +489,12 @@ public class Event<TSender, TArguments> where TSender : class
public delegate void EventHandler(TSender sender, TArguments args); public delegate void EventHandler(TSender sender, TArguments args);
private record struct ListenerData(EventHandler Callback, int Priority); private record struct ListenerData(EventHandler Callback, int Priority);
} }
internal static class EventHelpers
{
public static void LogInvocationException(object sender, ILogger logger, Exception exception, string methodCallRepresentation)
{
logger.LogException(sender, exception);
logger.LogError(sender, $"Unexpected exception on invocation of method {methodCallRepresentation}");
}
}