Skip to content

Commit 96a1233

Browse files
Merge pull request #215 from UchuServer/enhancement/sentry-logging
Implment sentry error logging for packets, gms and events
2 parents 3cfe5ef + 5c2dc62 commit 96a1233

File tree

9 files changed

+77
-83
lines changed

9 files changed

+77
-83
lines changed

Uchu.Core/Config/UchuConfiguration.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public class UchuConfiguration
2222
Password = "password"
2323
};
2424

25+
/// <summary>
26+
/// Optional sentry DSN to use for tracking errors and exceptions
27+
/// </summary>
28+
[XmlElement]
29+
public string SentryDsn { get; set; } = "";
30+
2531
/// <summary>
2632
/// The level of console logging (debug or production)
2733
/// </summary>

Uchu.Core/Uchu.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.2" />
2020
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.0-alpha.2" />
2121
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.2" />
22+
<PackageReference Include="Sentry" Version="3.0.6" />
2223
<PackageReference Include="StackExchange.Redis" Version="2.1.0-preview.23" />
2324
</ItemGroup>
2425

Uchu.Core/UchuServer.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using RakDotNet;
1616
using RakDotNet.IO;
1717
using RakDotNet.TcpUdp;
18+
using Sentry;
1819
using StackExchange.Redis;
1920
using Uchu.Api;
2021
using Uchu.Api.Models;
@@ -262,28 +263,31 @@ public async Task SetupApiAsync()
262263
/// <param name="acceptConsoleCommands">Whether to handle console commands or not</param>
263264
public async Task StartAsync(Assembly assembly, bool acceptConsoleCommands = false)
264265
{
265-
RegisterDefaultAssemblies(assembly);
266-
if (acceptConsoleCommands)
266+
using (SentrySdk.Init(Config.SentryDsn))
267267
{
268-
StartConsole();
269-
}
270-
271-
Running = true;
268+
RegisterDefaultAssemblies(assembly);
269+
if (acceptConsoleCommands)
270+
{
271+
StartConsole();
272+
}
273+
274+
Running = true;
272275

273-
var server = RakNetServer.RunAsync();
274-
var api = Api.StartAsync(ApiPort);
276+
var server = RakNetServer.RunAsync();
277+
var api = Api.StartAsync(ApiPort);
275278

276-
var tasks = new[]
277-
{
278-
server,
279-
api
280-
};
279+
var tasks = new[]
280+
{
281+
server,
282+
api
283+
};
281284

282-
await Task.WhenAny(tasks).ConfigureAwait(false);
283-
284-
Console.WriteLine($"EXIT: {server.Status} | {api.Status}");
285+
await Task.WhenAny(tasks).ConfigureAwait(false);
286+
287+
Console.WriteLine($"EXIT: {server.Status} | {api.Status}");
285288

286-
await StopAsync().ConfigureAwait(false);
289+
await StopAsync().ConfigureAwait(false);
290+
}
287291
}
288292

289293
/// <summary>
@@ -464,6 +468,7 @@ public async Task HandlePacketAsync(IPEndPoint endPoint, byte[] data, Reliabilit
464468
}
465469
catch (Exception e)
466470
{
471+
SentrySdk.CaptureException(e);
467472
Logger.Error($"Error when handling GM: {e}");
468473
}
469474
}
@@ -488,6 +493,7 @@ public async Task HandlePacketAsync(IPEndPoint endPoint, byte[] data, Reliabilit
488493
}
489494
catch (Exception e)
490495
{
496+
SentrySdk.CaptureException(e);
491497
Logger.Error($"Error when handling packet: {e}");
492498
}
493499
}

Uchu.World/Handlers/GameMessages/SkillHandler.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,13 @@ public class SkillHandler : HandlerGroup
1212
[PacketHandler]
1313
public async Task SkillStartHandler(StartSkillMessage message, Player player)
1414
{
15-
try
16-
{
17-
await player.GetComponent<SkillComponent>().StartUserSkillAsync(message);
18-
}
19-
catch (Exception e)
20-
{
21-
Logger.Error(e);
22-
}
15+
await player.GetComponent<SkillComponent>().StartUserSkillAsync(message);
2316
}
2417

2518
[PacketHandler]
2619
public async Task SyncSkillHandler(SyncSkillMessage message, Player player)
2720
{
28-
try
29-
{
3021
player.GetComponent<SkillComponent>().SyncUserSkillAsync(message);
31-
}
32-
catch (Exception e)
33-
{
34-
Logger.Error(e);
35-
}
3622
}
3723

3824
[PacketHandler]

Uchu.World/Objects/Zone.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using InfectedRose.Utilities;
1313
using RakDotNet;
1414
using RakDotNet.IO;
15+
using Sentry;
1516
using Uchu.Core;
1617
using Uchu.Core.Client;
1718
using Uchu.Physics;
@@ -721,6 +722,7 @@ private void UpdateObjects()
721722
catch (Exception e)
722723
{
723724
Logger.Error(e);
725+
SentrySdk.CaptureException(e);
724726
}
725727
}
726728

@@ -758,7 +760,8 @@ private void ExecuteSchedule()
758760
}
759761
catch (Exception e)
760762
{
761-
Logger.Error(e.Message);
763+
Logger.Error(e);
764+
SentrySdk.CaptureException(e);
762765
}
763766
finally
764767
{

Uchu.World/Systems/Behaviors/BehaviorTree.cs

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -456,17 +456,10 @@ public void Execute()
456456
if (!(Serialized || Deserialized))
457457
throw new InvalidOperationException("Can't execute tree: it's neither serialized nor deserialized.");
458458

459-
try
460-
{
461-
ExecuteRootBehaviorsForSkillType(SkillCastType.Default);
462-
463-
if (CastType != SkillCastType.Default)
464-
ExecuteRootBehaviorsForSkillType(CastType);
465-
}
466-
catch (Exception e)
467-
{
468-
Logger.Error($"Encountered error in tree execute: {e}");
469-
}
459+
ExecuteRootBehaviorsForSkillType(SkillCastType.Default);
460+
461+
if (CastType != SkillCastType.Default)
462+
ExecuteRootBehaviorsForSkillType(CastType);
470463
}
471464

472465
/// <summary>
@@ -479,17 +472,10 @@ public void Use()
479472
throw new InvalidOperationException("Can't use behavior: tree is not deserialized.");
480473
if (!RootBehaviors.TryGetValue(SkillCastType.OnUse, out var list))
481474
return;
482-
483-
try
484-
{
485-
foreach (var behaviorExecution in list)
486-
{
487-
behaviorExecution.BehaviorBase.ExecuteStart(behaviorExecution.BehaviorExecutionParameters);
488-
}
489-
}
490-
catch (Exception e)
475+
476+
foreach (var behaviorExecution in list)
491477
{
492-
Logger.Error($"Encountered error during tree use: {e.Message}");
478+
behaviorExecution.BehaviorBase.ExecuteStart(behaviorExecution.BehaviorExecutionParameters);
493479
}
494480
}
495481

@@ -504,18 +490,11 @@ public void Mount()
504490
throw new InvalidOperationException("Can't mount tree: tree is not deserialized.");
505491
if (!RootBehaviors.TryGetValue(SkillCastType.OnEquip, out var list))
506492
return;
507-
508-
try
509-
{
510-
foreach (var executionPreparation in list)
511-
{
512-
executionPreparation.BehaviorBase.ExecuteStart(executionPreparation
513-
.BehaviorExecutionParameters);
514-
}
515-
}
516-
catch (Exception e)
493+
494+
foreach (var executionPreparation in list)
517495
{
518-
Logger.Error($"Encountered error during tree mount: {e.Message}");
496+
executionPreparation.BehaviorBase.ExecuteStart(executionPreparation
497+
.BehaviorExecutionParameters);
519498
}
520499
}
521500

@@ -530,17 +509,10 @@ public void Dismantle()
530509
if (!RootBehaviors.TryGetValue(SkillCastType.OnEquip, out var list))
531510
return;
532511

533-
try
534-
{
535-
foreach (var executionPreparation in list)
536-
{
537-
executionPreparation.BehaviorBase.Dismantle(executionPreparation
538-
.BehaviorExecutionParameters);
539-
}
540-
}
541-
catch (Exception e)
512+
foreach (var executionPreparation in list)
542513
{
543-
Logger.Error($"Encountered error during tree dismantle: {e.Message}");
514+
executionPreparation.BehaviorBase.Dismantle(executionPreparation
515+
.BehaviorExecutionParameters);
544516
}
545517
}
546518
}

Uchu.World/Uchu.World.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@
2222
<Folder Include="Systems" />
2323
</ItemGroup>
2424

25+
<ItemGroup>
26+
<PackageReference Include="Sentry" Version="3.0.6" />
27+
</ItemGroup>
28+
2529
</Project>

Uchu.World/Utilities/Event.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using System.Threading.Tasks;
4+
using Sentry;
45
using Uchu.Core;
56

67
namespace Uchu.World
@@ -72,6 +73,7 @@ public void Invoke()
7273
catch (Exception e)
7374
{
7475
Logger.Error(e);
76+
SentrySdk.CaptureException(e);
7577
}
7678
}
7779

@@ -84,6 +86,7 @@ public void Invoke()
8486
catch (Exception e)
8587
{
8688
Logger.Error(e);
89+
SentrySdk.CaptureException(e);
8790
}
8891
}
8992
}
@@ -100,6 +103,7 @@ public async Task InvokeAsync()
100103
}
101104
catch (Exception e)
102105
{
106+
SentrySdk.CaptureException(e);
103107
Logger.Error(e);
104108
}
105109
}
@@ -112,6 +116,7 @@ public async Task InvokeAsync()
112116
}
113117
catch (Exception e)
114118
{
119+
SentrySdk.CaptureException(e);
115120
Logger.Error(e);
116121
}
117122
}
@@ -134,6 +139,7 @@ public void Invoke(T value)
134139
catch (Exception e)
135140
{
136141
Logger.Error(e);
142+
SentrySdk.CaptureException(e);
137143
}
138144
}
139145

@@ -146,6 +152,7 @@ public void Invoke(T value)
146152
catch (Exception e)
147153
{
148154
Logger.Error(e);
155+
SentrySdk.CaptureException(e);
149156
}
150157
}
151158
}
@@ -163,6 +170,7 @@ public async Task InvokeAsync(T value)
163170
catch (Exception e)
164171
{
165172
Logger.Error(e);
173+
SentrySdk.CaptureException(e);
166174
}
167175
}
168176

@@ -175,6 +183,7 @@ public async Task InvokeAsync(T value)
175183
catch (Exception e)
176184
{
177185
Logger.Error(e);
186+
SentrySdk.CaptureException(e);
178187
}
179188
}
180189
}
@@ -196,6 +205,7 @@ public void Invoke(T value, T2 value2)
196205
catch (Exception e)
197206
{
198207
Logger.Error(e);
208+
SentrySdk.CaptureException(e);
199209
}
200210
}
201211

@@ -208,6 +218,7 @@ public void Invoke(T value, T2 value2)
208218
catch (Exception e)
209219
{
210220
Logger.Error(e);
221+
SentrySdk.CaptureException(e);
211222
}
212223
}
213224
}
@@ -225,6 +236,7 @@ public async Task InvokeAsync(T value, T2 value2)
225236
catch (Exception e)
226237
{
227238
Logger.Error(e);
239+
SentrySdk.CaptureException(e);
228240
}
229241
}
230242

@@ -237,6 +249,7 @@ public async Task InvokeAsync(T value, T2 value2)
237249
catch (Exception e)
238250
{
239251
Logger.Error(e);
252+
SentrySdk.CaptureException(e);
240253
}
241254
}
242255
}
@@ -258,6 +271,7 @@ public void Invoke(T value, T2 value2, T3 value3)
258271
catch (Exception e)
259272
{
260273
Logger.Error(e);
274+
SentrySdk.CaptureException(e);
261275
}
262276
}
263277

@@ -270,6 +284,7 @@ public void Invoke(T value, T2 value2, T3 value3)
270284
catch (Exception e)
271285
{
272286
Logger.Error(e);
287+
SentrySdk.CaptureException(e);
273288
}
274289
}
275290
}
@@ -287,6 +302,7 @@ public async Task InvokeAsync(T value, T2 value2, T3 value3)
287302
catch (Exception e)
288303
{
289304
Logger.Error(e);
305+
SentrySdk.CaptureException(e);
290306
}
291307
}
292308

@@ -299,6 +315,7 @@ public async Task InvokeAsync(T value, T2 value2, T3 value3)
299315
catch (Exception e)
300316
{
301317
Logger.Error(e);
318+
SentrySdk.CaptureException(e);
302319
}
303320
}
304321
}

0 commit comments

Comments
 (0)