Skip to content

Commit 4873b1c

Browse files
committed
Update OnLogMessage.cs
#!components: grid-bot discord-net/Discord.Net#3128 ~ Implement a bit of code to shadow serializer exceptions, but allow viewing the error in debug situations
1 parent 817088c commit 4873b1c

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

services/grid-bot/lib/events/Events/OnLogMessage.cs

+27-4
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,22 @@ public class OnLogMessage
3939
"log_severity"
4040
);
4141

42+
private readonly Counter _totalSerializerErrors = Metrics.CreateCounter(
43+
"grid_discord_serializer_errors_total",
44+
"The total number of serializer errors."
45+
);
46+
47+
private const string _serializerErrorMessage = "Serializer Error";
48+
4249
// These are specific strings that fill the log files up drastically.
43-
private static readonly HashSet<string> _messagesToBeConsideredDebug = new()
44-
{
50+
private static readonly HashSet<string> _messagesToBeConsideredDebug =
51+
[
4552
"Disconnecting",
4653
"Disconnected",
4754
"Connecting",
4855
"Connected",
4956
"Resumed previous session"
50-
};
57+
];
5158

5259
#if DEBUG || DEBUG_LOGGING_IN_PROD
5360
/// <summary>
@@ -112,7 +119,23 @@ public Task Invoke(LogMessage message)
112119
return Task.CompletedTask;
113120
#endif
114121

115-
if (message.Exception is InteractionException or CommandException)
122+
// Temporary fix for discord-net/Discord.Net#3128
123+
// Just keep it out of Backtrace and increment a counter.
124+
if (message.Message == _serializerErrorMessage)
125+
{
126+
_totalSerializerErrors.Inc();
127+
128+
// Debug log the serializer error, in case we need to see it.
129+
_logger.Debug(
130+
"Serializer Error, Source = {0}, Exception = {1}",
131+
message.Source,
132+
message.Exception.ToString()
133+
);
134+
135+
return Task.CompletedTask;
136+
}
137+
138+
if (message.Exception is InteractionException or CommandException) // Handled by the command handler.
116139
return Task.CompletedTask;
117140

118141
_logger.Error(

0 commit comments

Comments
 (0)