Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Exiled::Events] (Addition) EventExceptionLogger to capture event exceptions. #2207

Merged
merged 3 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Exiled.Events/Features/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void InvokeSafely()
}
catch (Exception ex)
{
Log.Error($"Method \"{handler.Method.Name}\" of the class \"{handler.Method.ReflectedType.FullName}\" caused an exception when handling the event \"{GetType().FullName}\"\n{ex}");
EventExceptionLogger.CaptureException(ex, GetType().FullName, handler.Method);
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions Exiled.Events/Features/EventExceptionLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// -----------------------------------------------------------------------
// <copyright file="EventExceptionLogger.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.Features
{
using System;
using System.Reflection;

using Exiled.API.Features;

/// <summary>
/// A class that logs exceptions that occur when handling events.
/// </summary>
public static class EventExceptionLogger
{
/// <summary>
/// Logs an exception that occurred when handling an event.
/// </summary>
/// <param name="ex">The exception that occurred.</param>
/// <param name="eventName">The name of the event.</param>
/// <param name="method">The method that caused the exception.</param>
public static void CaptureException(Exception ex, string eventName, MethodInfo method)
{
Log.Error($"Method \"{method.Name}\" of the class \"{method.ReflectedType.FullName}\" caused an exception when handling the event \"{eventName}\"\n{ex}");
}
}
}
2 changes: 1 addition & 1 deletion Exiled.Events/Features/Event{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void InvokeSafely(T arg)
}
catch (Exception ex)
{
Log.Error($"Method \"{handler.Method.Name}\" of the class \"{handler.Method.ReflectedType.FullName}\" caused an exception when handling the event \"{GetType().FullName}\"\n{ex}");
EventExceptionLogger.CaptureException(ex, GetType().FullName, handler.Method);
}
}
}
Expand Down
Loading