diff --git a/TwitchLib.EventSub.Websockets/EventSubWebsocketClient.cs b/TwitchLib.EventSub.Websockets/EventSubWebsocketClient.cs index 2833dfa..6cf0721 100644 --- a/TwitchLib.EventSub.Websockets/EventSubWebsocketClient.cs +++ b/TwitchLib.EventSub.Websockets/EventSubWebsocketClient.cs @@ -622,7 +622,7 @@ private async Task HandleDisconnect(string message) if (data != null) _logger?.LogForceDisconnected(data.Payload.Session.Id, data.Payload.Session.DisconnectedAt, data.Payload.Session.DisconnectReason); - + await WebsocketDisconnected.InvokeAsync(this, EventArgs.Empty); } @@ -665,16 +665,26 @@ private void HandleRevocation(string message) /// /// name of the event to raise /// args to pass with the event - internal void RaiseEvent(string eventName, object args = null) + internal async void RaiseEvent(string eventName, object args = null) { var fInfo = GetType().GetField(eventName, BindingFlags.Instance | BindingFlags.NonPublic); if (fInfo?.GetValue(this) is not MulticastDelegate multi) return; + var parameters = new object[] { this, args ?? EventArgs.Empty }; foreach (var del in multi.GetInvocationList()) { - del.Method.Invoke(del.Target, args == null ? new object[] { this, EventArgs.Empty } : new[] { this, args }); + try + { + var result = del.Method.Invoke(del.Target, parameters); + if (result is Task task) + await task; + } + catch (Exception ex) + { + _logger.LogRaiseEventExeption(eventName, ex); + } } } } diff --git a/TwitchLib.EventSub.Websockets/Extensions/LogExtensions.cs b/TwitchLib.EventSub.Websockets/Extensions/LogExtensions.cs index 31656e3..2e73160 100644 --- a/TwitchLib.EventSub.Websockets/Extensions/LogExtensions.cs +++ b/TwitchLib.EventSub.Websockets/Extensions/LogExtensions.cs @@ -7,9 +7,12 @@ namespace TwitchLib.EventSub.Websockets.Extensions { internal static partial class LogExtensions { + [LoggerMessage(LogLevel.Error, "Exeption was throw when raising '{eventName}' event.")] + public static partial void LogRaiseEventExeption(this ILogger logger, string eventName, Exception ex); + [LoggerMessage(LogLevel.Debug, "{message}")] public static partial void LogMessage(this ILogger logger, string message); - + [LoggerMessage(LogLevel.Critical, "Websocket {sessionId} disconnected at {disconnectedAt}. Reason: {disconnectReason}")] public static partial void LogForceDisconnected(this ILogger logger, string sessionId, DateTime? disconnectedAt, string disconnectReason); @@ -25,4 +28,4 @@ internal static partial class LogExtensions [LoggerMessage(LogLevel.Critical, "{closeStatus} - {closeStatusDescription}")] public static partial void LogWebsocketClosed(this ILogger logger, WebSocketCloseStatus closeStatus, string closeStatusDescription); } -} \ No newline at end of file +}