Skip to content

Commit 79f966d

Browse files
authored
Implement loggers (#115)
* Implement loggers replacing event based logging * Add a missing factory * Cleanup * Fix a disconnected log * Improve voice client logs * Improve RestClient logs * Improve websocket logs * Improve rate limited exceptions * Improve loggers * Update guides * Cleanup * Remove useless code * Cleanup * Minor logging improvements * Improve handler exception logging * Update logs of ApplicationCommandServiceHostedService * Improve logging of NetCord.Hosting.AspNetCore * Normalize exception formatting
1 parent 3bb7932 commit 79f966d

File tree

71 files changed

+1003
-471
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1003
-471
lines changed
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
using NetCord;
22
using NetCord.Gateway;
3+
using NetCord.Logging;
34

4-
ShardedGatewayClient client = new(new BotToken("Token from Discord Developer Portal"));
5-
6-
client.Log += (client, message) =>
5+
ShardedGatewayClient client = new(new BotToken("Token from Discord Developer Portal"), new ShardedGatewayClientConfiguration
76
{
8-
Console.WriteLine($"#{client.Shard.GetValueOrDefault().Id}\t{message}");
9-
return default;
10-
};
7+
LoggerFactory = ShardedConsoleLogger.GetFactory(),
8+
});
119

1210
await client.StartAsync();
1311
await Task.Delay(-1);

Documentation/guides/basic-concepts/Sharding/RegisteringHandlers.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
using NetCord;
22
using NetCord.Gateway;
3+
using NetCord.Logging;
34

45
namespace MyBot;
56

67
internal class RegisteringHandlers
78
{
89
public static async Task RegisterHandlersAsync()
910
{
10-
ShardedGatewayClient client = new(new BotToken("Token from Discord Developer Portal"));
11-
12-
client.Log += (client, message) =>
11+
ShardedGatewayClient client = new(new BotToken("Token from Discord Developer Portal"), new ShardedGatewayClientConfiguration
1312
{
14-
Console.WriteLine($"#{client.Shard.GetValueOrDefault().Id}\t{message}");
15-
return default;
16-
};
13+
LoggerFactory = ShardedConsoleLogger.GetFactory(),
14+
});
1715

1816
client.MessageUpdate += async (client, message) =>
1917
{

Documentation/guides/basic-concepts/Voice/Program.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using NetCord;
22
using NetCord.Gateway;
3+
using NetCord.Logging;
34
using NetCord.Rest;
45
using NetCord.Services;
56
using NetCord.Services.ApplicationCommands;
67

7-
GatewayClient client = new(new BotToken("Token from Discord Developer Portal"), new GatewayClientConfiguration()
8+
GatewayClient client = new(new BotToken("Token from Discord Developer Portal"), new GatewayClientConfiguration
89
{
910
Intents = GatewayIntents.Guilds | GatewayIntents.GuildVoiceStates,
11+
Logger = new ConsoleLogger(),
1012
});
1113

1214
ApplicationCommandService<ApplicationCommandContext> applicationCommandService = new();
@@ -33,11 +35,5 @@
3335

3436
await applicationCommandService.CreateCommandsAsync(client.Rest, client.Id);
3537

36-
client.Log += message =>
37-
{
38-
Console.WriteLine(message);
39-
return default;
40-
};
41-
4238
await client.StartAsync();
4339
await Task.Delay(-1);

Documentation/guides/basic-concepts/Voice/VoiceModule.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using NetCord;
44
using NetCord.Gateway.Voice;
5+
using NetCord.Logging;
56
using NetCord.Rest;
67
using NetCord.Services.ApplicationCommands;
78

@@ -35,7 +36,11 @@ public async Task PlayAsync(string track)
3536
// You also need to add a synchronization here. 'JoinVoiceChannelAsync' should not be used concurrently for the same guild
3637
var voiceClient = await client.JoinVoiceChannelAsync(
3738
guild.Id,
38-
voiceState.ChannelId.GetValueOrDefault());
39+
voiceState.ChannelId.GetValueOrDefault(),
40+
new VoiceClientConfiguration
41+
{
42+
Logger = new ConsoleLogger(),
43+
});
3944

4045
// Connect
4146
await voiceClient.StartAsync();
@@ -122,7 +127,11 @@ public async Task<string> EchoAsync()
122127
var voiceClient = await client.JoinVoiceChannelAsync(
123128
guild.Id,
124129
voiceState.ChannelId.GetValueOrDefault(),
125-
new() { RedirectInputStreams = true /* Required to receive voice */ });
130+
new VoiceClientConfiguration
131+
{
132+
RedirectInputStreams = true, // Required to receive voice
133+
Logger = new ConsoleLogger(),
134+
});
126135

127136
// Connect
128137
await voiceClient.StartAsync();

Documentation/guides/basic-concepts/sharding.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ When creating event handlers, implement @NetCord.Hosting.Gateway.IShardedGateway
4141
### [Bare Bones](#tab/bare-bones)
4242

4343
For bare-bones setups, adding event handlers is straightforward. Each handler has an additional parameter for the @NetCord.Gateway.GatewayClient that received the event.
44-
[!code-cs[Program.cs](Sharding/RegisteringHandlers.cs#L18-L21)]
44+
[!code-cs[Program.cs](Sharding/RegisteringHandlers.cs#L16-L19)]

Documentation/guides/basic-concepts/voice.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Follow the [installation guide](installing-native-dependencies.md) to install th
1010
> In the following examples streams and @NetCord.Gateway.Voice.VoiceClient instances are not disposed because they should be stored somewhere and disposed later.
1111
1212
### Sending Voice
13-
[!code-cs[VoiceModule.cs](Voice/VoiceModule.cs#L12-L105)]
13+
[!code-cs[VoiceModule.cs](Voice/VoiceModule.cs#L13-L110)]
1414

1515
### Receiving Voice
16-
[!code-cs[VoiceModule.cs](Voice/VoiceModule.cs#L107-L146)]
16+
[!code-cs[VoiceModule.cs](Voice/VoiceModule.cs#L112-L155)]
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
using NetCord;
22
using NetCord.Gateway;
3+
using NetCord.Logging;
34

4-
GatewayClient client = new(new BotToken("Token from Discord Developer Portal"));
5-
6-
client.Log += message =>
5+
GatewayClient client = new(new BotToken("Token from Discord Developer Portal"), new GatewayClientConfiguration
76
{
8-
Console.WriteLine(message);
9-
return default;
10-
};
7+
Logger = new ConsoleLogger(),
8+
});
119

1210
await client.StartAsync();
1311
await Task.Delay(-1);

Documentation/guides/getting-started/making-a-bot.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,12 @@ For better configuration management, store the token in an `appsettings.json` fi
7272
#### [Bare Bones](#tab/bare-bones)
7373

7474
To create the @NetCord.Gateway.GatewayClient manually, add the following lines to your `Program.cs` file.
75-
[!code-cs[Program.cs](Coding/Program.cs#L1-L4)]
75+
[!code-cs[Program.cs](Coding/Program.cs#L1-L8)]
7676

77-
Next, you can add logging to track your bot's activity.
78-
[!code-cs[Program.cs](Coding/Program.cs#L6-L10)]
77+
We have also added a console logger to log messages to the console. This is useful for debugging and monitoring your bot's activity.
7978

8079
Finally, start your bot with the following lines.
81-
[!code-cs[Program.cs](Coding/Program.cs#L12-L13)]
80+
[!code-cs[Program.cs](Coding/Program.cs#L10-L11)]
8281

8382
#### The Final Product
8483
[!code-cs[Program.cs](Coding/Program.cs)]

Documentation/guides/services/application-commands/Introduction/Program.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using NetCord;
22
using NetCord.Gateway;
3+
using NetCord.Logging;
34
using NetCord.Rest;
45
using NetCord.Services;
56
using NetCord.Services.ApplicationCommands;
67

7-
GatewayClient client = new(new BotToken("Token from Discord Developer Portal"), new GatewayClientConfiguration()
8+
GatewayClient client = new(new BotToken("Token from Discord Developer Portal"), new GatewayClientConfiguration
89
{
910
Intents = default,
11+
Logger = new ConsoleLogger(),
1012
});
1113

1214
// Create the application command service
@@ -47,11 +49,5 @@
4749
// Create the commands so that you can use them in the Discord client
4850
await applicationCommandService.CreateCommandsAsync(client.Rest, client.Id);
4951

50-
client.Log += message =>
51-
{
52-
Console.WriteLine(message);
53-
return default;
54-
};
55-
5652
await client.StartAsync();
5753
await Task.Delay(-1);

Documentation/guides/services/application-commands/Localizations/Program.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using NetCord;
22
using NetCord.Gateway;
3+
using NetCord.Logging;
34
using NetCord.Rest;
45
using NetCord.Services;
56
using NetCord.Services.ApplicationCommands;
67

7-
GatewayClient client = new(new BotToken("Token from Discord Developer Portal"), new GatewayClientConfiguration()
8+
GatewayClient client = new(new BotToken("Token from Discord Developer Portal"), new GatewayClientConfiguration
89
{
910
Intents = default,
11+
Logger = new ConsoleLogger(),
1012
});
1113

1214
ApplicationCommandService<ApplicationCommandContext> applicationCommandService = new(ApplicationCommandServiceConfiguration<ApplicationCommandContext>.Default with
@@ -37,11 +39,5 @@
3739

3840
await applicationCommandService.CreateCommandsAsync(client.Rest, client.Id);
3941

40-
client.Log += message =>
41-
{
42-
Console.WriteLine(message);
43-
return default;
44-
};
45-
4642
await client.StartAsync();
4743
await Task.Delay(-1);

0 commit comments

Comments
 (0)