Browse Source

Use ilogger instead of old logger

pull/1765/head
Waterball 4 years ago
parent
commit
362a972213
7 changed files with 251 additions and 180 deletions
  1. +22
    -13
      src/Discord.Net.Rest/BaseDiscordClient.cs
  2. +54
    -37
      src/Discord.Net.WebSocket/Audio/AudioClient.cs
  3. +7
    -6
      src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs
  4. +10
    -9
      src/Discord.Net.WebSocket/ConnectionManager.cs
  5. +0
    -1
      src/Discord.Net.WebSocket/DiscordShardedClient.cs
  6. +137
    -103
      src/Discord.Net.WebSocket/DiscordSocketClient.cs
  7. +21
    -11
      src/Discord.Net.Webhook/DiscordWebhookClient.cs

+ 22
- 13
src/Discord.Net.Rest/BaseDiscordClient.cs View File

@@ -1,4 +1,5 @@
using Discord.Logging; using Discord.Logging;
using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
@@ -10,20 +11,17 @@ namespace Discord.Rest
{ {
public abstract class BaseDiscordClient : IDiscordClient public abstract class BaseDiscordClient : IDiscordClient
{ {
public event Func<LogMessage, Task> Log { add { _logEvent.Add(value); } remove { _logEvent.Remove(value); } }
internal readonly AsyncEvent<Func<LogMessage, Task>> _logEvent = new AsyncEvent<Func<LogMessage, Task>>();

public event Func<Task> LoggedIn { add { _loggedInEvent.Add(value); } remove { _loggedInEvent.Remove(value); } } public event Func<Task> LoggedIn { add { _loggedInEvent.Add(value); } remove { _loggedInEvent.Remove(value); } }
private readonly AsyncEvent<Func<Task>> _loggedInEvent = new AsyncEvent<Func<Task>>(); private readonly AsyncEvent<Func<Task>> _loggedInEvent = new AsyncEvent<Func<Task>>();
public event Func<Task> LoggedOut { add { _loggedOutEvent.Add(value); } remove { _loggedOutEvent.Remove(value); } } public event Func<Task> LoggedOut { add { _loggedOutEvent.Add(value); } remove { _loggedOutEvent.Remove(value); } }
private readonly AsyncEvent<Func<Task>> _loggedOutEvent = new AsyncEvent<Func<Task>>(); private readonly AsyncEvent<Func<Task>> _loggedOutEvent = new AsyncEvent<Func<Task>>();


internal readonly Logger _restLogger;
internal readonly ILogger _restLogger;
private readonly SemaphoreSlim _stateLock; private readonly SemaphoreSlim _stateLock;
private bool _isFirstLogin, _isDisposed; private bool _isFirstLogin, _isDisposed;


internal API.DiscordRestApiClient ApiClient { get; } internal API.DiscordRestApiClient ApiClient { get; }
internal LogManager LogManager { get; }
internal ILoggerFactory LogManager { get; }
/// <summary> /// <summary>
/// Gets the login state of the client. /// Gets the login state of the client.
/// </summary> /// </summary>
@@ -39,21 +37,32 @@ namespace Discord.Rest
internal BaseDiscordClient(DiscordRestConfig config, API.DiscordRestApiClient client) internal BaseDiscordClient(DiscordRestConfig config, API.DiscordRestApiClient client)
{ {
ApiClient = client; ApiClient = client;
LogManager = new LogManager(config.LogLevel);
LogManager.Message += async msg => await _logEvent.InvokeAsync(msg).ConfigureAwait(false);
LogManager = config.LoggerFactory;

if (LogManager == null)
{
LogManager = new DefaultLoggerFactory();
LogManager.AddProvider(new DefaultLoggerProvider());
}


_stateLock = new SemaphoreSlim(1, 1); _stateLock = new SemaphoreSlim(1, 1);
_restLogger = LogManager.CreateLogger("Rest"); _restLogger = LogManager.CreateLogger("Rest");
_isFirstLogin = config.DisplayInitialLog; _isFirstLogin = config.DisplayInitialLog;


ApiClient.RequestQueue.RateLimitTriggered += async (id, info, endpoint) =>
ApiClient.RequestQueue.RateLimitTriggered += (id, info, endpoint) =>
{ {
if (info == null) if (info == null)
await _restLogger.VerboseAsync($"Preemptive Rate limit triggered: {endpoint} {(id.IsHashBucket ? $"(Bucket: {id.BucketHash})" : "")}").ConfigureAwait(false);
_restLogger.LogTrace($"Preemptive Rate limit triggered: {endpoint} {(id.IsHashBucket ? $"(Bucket: {id.BucketHash})" : "")}");
else else
await _restLogger.WarningAsync($"Rate limit triggered: {endpoint} {(id.IsHashBucket ? $"(Bucket: {id.BucketHash})" : "")}").ConfigureAwait(false);
_restLogger.LogTrace($"Rate limit triggered: {endpoint} {(id.IsHashBucket ? $"(Bucket: {id.BucketHash})" : "")}");

return Task.CompletedTask;
};
ApiClient.SentRequest += (method, endpoint, millis) =>
{
_restLogger.LogTrace($"{method} {endpoint}: {millis} ms");
return Task.CompletedTask;
}; };
ApiClient.SentRequest += async (method, endpoint, millis) => await _restLogger.VerboseAsync($"{method} {endpoint}: {millis} ms").ConfigureAwait(false);
} }


public async Task LoginAsync(TokenType tokenType, string token, bool validateToken = true) public async Task LoginAsync(TokenType tokenType, string token, bool validateToken = true)
@@ -70,7 +79,7 @@ namespace Discord.Rest
if (_isFirstLogin) if (_isFirstLogin)
{ {
_isFirstLogin = false; _isFirstLogin = false;
await LogManager.WriteInitialLog().ConfigureAwait(false);
_restLogger.LogInformation($"Discord.Net v{DiscordConfig.Version} (API v{DiscordConfig.APIVersion})");
} }


if (LoginState != LoginState.LoggedOut) if (LoginState != LoginState.LoggedOut)
@@ -90,7 +99,7 @@ namespace Discord.Rest
catch (ArgumentException ex) catch (ArgumentException ex)
{ {
// log these ArgumentExceptions and allow for the client to attempt to log in anyways // log these ArgumentExceptions and allow for the client to attempt to log in anyways
await LogManager.WarningAsync("Discord", "A supplied token was invalid.", ex).ConfigureAwait(false);
_restLogger.LogWarning(ex, "A supplied token was invalid.");
} }
} }




+ 54
- 37
src/Discord.Net.WebSocket/Audio/AudioClient.cs View File

@@ -3,6 +3,7 @@ using Discord.Audio.Streams;
using Discord.Logging; using Discord.Logging;
using Discord.Net.Converters; using Discord.Net.Converters;
using Discord.WebSocket; using Discord.WebSocket;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System; using System;
@@ -30,7 +31,7 @@ namespace Discord.Audio
} }
} }


private readonly Logger _audioLogger;
private readonly ILogger _audioLogger;
private readonly JsonSerializer _serializer; private readonly JsonSerializer _serializer;
private readonly ConnectionManager _connection; private readonly ConnectionManager _connection;
private readonly SemaphoreSlim _stateLock; private readonly SemaphoreSlim _stateLock;
@@ -64,8 +65,16 @@ namespace Discord.Audio
_audioLogger = Discord.LogManager.CreateLogger($"Audio #{clientId}"); _audioLogger = Discord.LogManager.CreateLogger($"Audio #{clientId}");


ApiClient = new DiscordVoiceAPIClient(guild.Id, Discord.WebSocketProvider, Discord.UdpSocketProvider); ApiClient = new DiscordVoiceAPIClient(guild.Id, Discord.WebSocketProvider, Discord.UdpSocketProvider);
ApiClient.SentGatewayMessage += async opCode => await _audioLogger.DebugAsync($"Sent {opCode}").ConfigureAwait(false);
ApiClient.SentDiscovery += async () => await _audioLogger.DebugAsync("Sent Discovery").ConfigureAwait(false);
ApiClient.SentGatewayMessage += opCode =>
{
_audioLogger.LogDebug($"Sent {opCode}");
return Task.CompletedTask;
};
ApiClient.SentDiscovery += () =>
{
_audioLogger.LogDebug("Sent Discovery");
return Task.CompletedTask;
};
//ApiClient.SentData += async bytes => await _audioLogger.DebugAsync($"Sent {bytes} Bytes").ConfigureAwait(false); //ApiClient.SentData += async bytes => await _audioLogger.DebugAsync($"Sent {bytes} Bytes").ConfigureAwait(false);
ApiClient.ReceivedEvent += ProcessMessageAsync; ApiClient.ReceivedEvent += ProcessMessageAsync;
ApiClient.ReceivedPacket += ProcessPacketAsync; ApiClient.ReceivedPacket += ProcessPacketAsync;
@@ -83,12 +92,20 @@ namespace Discord.Audio
_serializer = new JsonSerializer { ContractResolver = new DiscordContractResolver() }; _serializer = new JsonSerializer { ContractResolver = new DiscordContractResolver() };
_serializer.Error += (s, e) => _serializer.Error += (s, e) =>
{ {
_audioLogger.WarningAsync(e.ErrorContext.Error).GetAwaiter().GetResult();
_audioLogger.LogWarning(e.ErrorContext.Error, e.ErrorContext.Error.Message);
e.ErrorContext.Handled = true; e.ErrorContext.Handled = true;
}; };


LatencyUpdated += async (old, val) => await _audioLogger.DebugAsync($"Latency = {val} ms").ConfigureAwait(false);
UdpLatencyUpdated += async (old, val) => await _audioLogger.DebugAsync($"UDP Latency = {val} ms").ConfigureAwait(false);
LatencyUpdated += (old, val) =>
{
_audioLogger.LogDebug($"Latency = {val} ms");
return Task.CompletedTask;
};
UdpLatencyUpdated += (old, val) =>
{
_audioLogger.LogDebug($"UDP Latency = {val} ms");
return Task.CompletedTask;
};
} }


internal async Task StartAsync(string url, ulong userId, string sessionId, string token) internal async Task StartAsync(string url, ulong userId, string sessionId, string token)
@@ -112,10 +129,10 @@ namespace Discord.Audio


private async Task OnConnectingAsync() private async Task OnConnectingAsync()
{ {
await _audioLogger.DebugAsync("Connecting ApiClient").ConfigureAwait(false);
_audioLogger.LogDebug("Connecting ApiClient");
await ApiClient.ConnectAsync("wss://" + _url + "?v=" + DiscordConfig.VoiceAPIVersion).ConfigureAwait(false); await ApiClient.ConnectAsync("wss://" + _url + "?v=" + DiscordConfig.VoiceAPIVersion).ConfigureAwait(false);
await _audioLogger.DebugAsync("Listening on port " + ApiClient.UdpPort).ConfigureAwait(false);
await _audioLogger.DebugAsync("Sending Identity").ConfigureAwait(false);
_audioLogger.LogDebug("Listening on port " + ApiClient.UdpPort);
_audioLogger.LogDebug("Sending Identity");
await ApiClient.SendIdentityAsync(_userId, _sessionId, _token).ConfigureAwait(false); await ApiClient.SendIdentityAsync(_userId, _sessionId, _token).ConfigureAwait(false);


//Wait for READY //Wait for READY
@@ -123,11 +140,11 @@ namespace Discord.Audio
} }
private async Task OnDisconnectingAsync(Exception ex) private async Task OnDisconnectingAsync(Exception ex)
{ {
await _audioLogger.DebugAsync("Disconnecting ApiClient").ConfigureAwait(false);
_audioLogger.LogDebug("Disconnecting ApiClient");
await ApiClient.DisconnectAsync().ConfigureAwait(false); await ApiClient.DisconnectAsync().ConfigureAwait(false);


//Wait for tasks to complete //Wait for tasks to complete
await _audioLogger.DebugAsync("Waiting for heartbeater").ConfigureAwait(false);
_audioLogger.LogDebug("Waiting for heartbeater");
var heartbeatTask = _heartbeatTask; var heartbeatTask = _heartbeatTask;
if (heartbeatTask != null) if (heartbeatTask != null)
await heartbeatTask.ConfigureAwait(false); await heartbeatTask.ConfigureAwait(false);
@@ -142,7 +159,7 @@ namespace Discord.Audio


await ClearInputStreamsAsync().ConfigureAwait(false); await ClearInputStreamsAsync().ConfigureAwait(false);


await _audioLogger.DebugAsync("Sending Voice State").ConfigureAwait(false);
_audioLogger.LogDebug("Sending Voice State");
await Discord.ApiClient.SendVoiceStateUpdateAsync(Guild.Id, null, false, false).ConfigureAwait(false); await Discord.ApiClient.SendVoiceStateUpdateAsync(Guild.Id, null, false, false).ConfigureAwait(false);
} }


@@ -224,7 +241,7 @@ namespace Discord.Audio
{ {
case VoiceOpCode.Ready: case VoiceOpCode.Ready:
{ {
await _audioLogger.DebugAsync("Received Ready").ConfigureAwait(false);
_audioLogger.LogDebug("Received Ready");
var data = (payload as JToken).ToObject<ReadyEvent>(_serializer); var data = (payload as JToken).ToObject<ReadyEvent>(_serializer);


_ssrc = data.SSRC; _ssrc = data.SSRC;
@@ -241,7 +258,7 @@ namespace Discord.Audio
break; break;
case VoiceOpCode.SessionDescription: case VoiceOpCode.SessionDescription:
{ {
await _audioLogger.DebugAsync("Received SessionDescription").ConfigureAwait(false);
_audioLogger.LogDebug("Received SessionDescription");
var data = (payload as JToken).ToObject<SessionDescriptionEvent>(_serializer); var data = (payload as JToken).ToObject<SessionDescriptionEvent>(_serializer);


if (data.Mode != DiscordVoiceAPIClient.Mode) if (data.Mode != DiscordVoiceAPIClient.Mode)
@@ -257,7 +274,7 @@ namespace Discord.Audio
break; break;
case VoiceOpCode.HeartbeatAck: case VoiceOpCode.HeartbeatAck:
{ {
await _audioLogger.DebugAsync("Received HeartbeatAck").ConfigureAwait(false);
_audioLogger.LogDebug("Received HeartbeatAck");


if (_heartbeatTimes.TryDequeue(out long time)) if (_heartbeatTimes.TryDequeue(out long time))
{ {
@@ -271,7 +288,7 @@ namespace Discord.Audio
break; break;
case VoiceOpCode.Speaking: case VoiceOpCode.Speaking:
{ {
await _audioLogger.DebugAsync("Received Speaking").ConfigureAwait(false);
_audioLogger.LogDebug("Received Speaking");


var data = (payload as JToken).ToObject<SpeakingEvent>(_serializer); var data = (payload as JToken).ToObject<SpeakingEvent>(_serializer);
_ssrcMap[data.Ssrc] = data.UserId; //TODO: Memory Leak: SSRCs are never cleaned up _ssrcMap[data.Ssrc] = data.UserId; //TODO: Memory Leak: SSRCs are never cleaned up
@@ -280,13 +297,13 @@ namespace Discord.Audio
} }
break; break;
default: default:
await _audioLogger.WarningAsync($"Unknown OpCode ({opCode})").ConfigureAwait(false);
_audioLogger.LogWarning($"Unknown OpCode ({opCode})");
return; return;
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
await _audioLogger.ErrorAsync($"Error handling {opCode}", ex).ConfigureAwait(false);
_audioLogger.LogError($"Error handling {opCode}", ex);
return; return;
} }
} }
@@ -298,7 +315,7 @@ namespace Discord.Audio
{ {
if (packet.Length != 70) if (packet.Length != 70)
{ {
await _audioLogger.DebugAsync("Malformed Packet").ConfigureAwait(false);
_audioLogger.LogDebug("Malformed Packet");
return; return;
} }
string ip; string ip;
@@ -310,18 +327,18 @@ namespace Discord.Audio
} }
catch (Exception ex) catch (Exception ex)
{ {
await _audioLogger.DebugAsync("Malformed Packet", ex).ConfigureAwait(false);
_audioLogger.LogDebug("Malformed Packet", ex);
return; return;
} }


await _audioLogger.DebugAsync("Received Discovery").ConfigureAwait(false);
_audioLogger.LogDebug("Received Discovery");
await ApiClient.SendSelectProtocol(ip, port).ConfigureAwait(false); await ApiClient.SendSelectProtocol(ip, port).ConfigureAwait(false);
} }
else if (_connection.State == ConnectionState.Connected) else if (_connection.State == ConnectionState.Connected)
{ {
if (packet.Length == 8) if (packet.Length == 8)
{ {
await _audioLogger.DebugAsync("Received Keepalive").ConfigureAwait(false);
_audioLogger.LogDebug("Received Keepalive");


ulong value = ulong value =
((ulong)packet[0] >> 0) | ((ulong)packet[0] >> 0) |
@@ -350,17 +367,17 @@ namespace Discord.Audio
{ {
if (!RTPReadStream.TryReadSsrc(packet, 0, out var ssrc)) if (!RTPReadStream.TryReadSsrc(packet, 0, out var ssrc))
{ {
await _audioLogger.DebugAsync("Malformed Frame").ConfigureAwait(false);
_audioLogger.LogDebug("Malformed Frame");
return; return;
} }
if (!_ssrcMap.TryGetValue(ssrc, out var userId)) if (!_ssrcMap.TryGetValue(ssrc, out var userId))
{ {
await _audioLogger.DebugAsync($"Unknown SSRC {ssrc}").ConfigureAwait(false);
_audioLogger.LogDebug($"Unknown SSRC {ssrc}");
return; return;
} }
if (!_streams.TryGetValue(userId, out var pair)) if (!_streams.TryGetValue(userId, out var pair))
{ {
await _audioLogger.DebugAsync($"Unknown User {userId}").ConfigureAwait(false);
_audioLogger.LogDebug($"Unknown User {userId}");
return; return;
} }
try try
@@ -369,7 +386,7 @@ namespace Discord.Audio
} }
catch (Exception ex) catch (Exception ex)
{ {
await _audioLogger.DebugAsync("Malformed Frame", ex).ConfigureAwait(false);
_audioLogger.LogDebug(ex, "Malformed Frame");
return; return;
} }
//await _audioLogger.DebugAsync($"Received {packet.Length} bytes from user {userId}").ConfigureAwait(false); //await _audioLogger.DebugAsync($"Received {packet.Length} bytes from user {userId}").ConfigureAwait(false);
@@ -378,7 +395,7 @@ namespace Discord.Audio
} }
catch (Exception ex) catch (Exception ex)
{ {
await _audioLogger.WarningAsync("Failed to process UDP packet", ex).ConfigureAwait(false);
_audioLogger.LogWarning("Failed to process UDP packet", ex);
return; return;
} }
} }
@@ -388,7 +405,7 @@ namespace Discord.Audio
// TODO: Clean this up when Discord's session patch is live // TODO: Clean this up when Discord's session patch is live
try try
{ {
await _audioLogger.DebugAsync("Heartbeat Started").ConfigureAwait(false);
_audioLogger.LogDebug("Heartbeat Started");
while (!cancelToken.IsCancellationRequested) while (!cancelToken.IsCancellationRequested)
{ {
var now = Environment.TickCount; var now = Environment.TickCount;
@@ -408,27 +425,27 @@ namespace Discord.Audio
} }
catch (Exception ex) catch (Exception ex)
{ {
await _audioLogger.WarningAsync("Failed to send heartbeat", ex).ConfigureAwait(false);
_audioLogger.LogWarning("Failed to send heartbeat", ex);
} }


await Task.Delay(intervalMillis, cancelToken).ConfigureAwait(false); await Task.Delay(intervalMillis, cancelToken).ConfigureAwait(false);
} }
await _audioLogger.DebugAsync("Heartbeat Stopped").ConfigureAwait(false);
_audioLogger.LogDebug("Heartbeat Stopped");
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
await _audioLogger.DebugAsync("Heartbeat Stopped").ConfigureAwait(false);
_audioLogger.LogDebug("Heartbeat Stopped");
} }
catch (Exception ex) catch (Exception ex)
{ {
await _audioLogger.ErrorAsync("Heartbeat Errored", ex).ConfigureAwait(false);
_audioLogger.LogError("Heartbeat Errored", ex);
} }
} }
private async Task RunKeepaliveAsync(int intervalMillis, CancellationToken cancelToken) private async Task RunKeepaliveAsync(int intervalMillis, CancellationToken cancelToken)
{ {
try try
{ {
await _audioLogger.DebugAsync("Keepalive Started").ConfigureAwait(false);
_audioLogger.LogDebug("Keepalive Started");
while (!cancelToken.IsCancellationRequested) while (!cancelToken.IsCancellationRequested)
{ {
var now = Environment.TickCount; var now = Environment.TickCount;
@@ -441,20 +458,20 @@ namespace Discord.Audio
} }
catch (Exception ex) catch (Exception ex)
{ {
await _audioLogger.WarningAsync("Failed to send keepalive", ex).ConfigureAwait(false);
_audioLogger.LogWarning("Failed to send keepalive", ex);
} }


await Task.Delay(intervalMillis, cancelToken).ConfigureAwait(false); await Task.Delay(intervalMillis, cancelToken).ConfigureAwait(false);
} }
await _audioLogger.DebugAsync("Keepalive Stopped").ConfigureAwait(false);
_audioLogger.LogDebug("Keepalive Stopped");
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
await _audioLogger.DebugAsync("Keepalive Stopped").ConfigureAwait(false);
_audioLogger.LogDebug("Keepalive Stopped");
} }
catch (Exception ex) catch (Exception ex)
{ {
await _audioLogger.ErrorAsync("Keepalive Errored", ex).ConfigureAwait(false);
_audioLogger.LogError("Keepalive Errored", ex);
} }
} }




+ 7
- 6
src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs View File

@@ -1,4 +1,5 @@
using Discord.Logging; using Discord.Logging;
using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Threading; using System.Threading;
@@ -33,14 +34,14 @@ namespace Discord.Audio.Streams
private readonly ConcurrentQueue<Frame> _queuedFrames; private readonly ConcurrentQueue<Frame> _queuedFrames;
private readonly ConcurrentQueue<byte[]> _bufferPool; private readonly ConcurrentQueue<byte[]> _bufferPool;
private readonly SemaphoreSlim _queueLock; private readonly SemaphoreSlim _queueLock;
private readonly Logger _logger;
private readonly ILogger _logger;
private readonly int _ticksPerFrame, _queueLength; private readonly int _ticksPerFrame, _queueLength;
private bool _isPreloaded; private bool _isPreloaded;
private int _silenceFrames; private int _silenceFrames;


public BufferedWriteStream(AudioStream next, IAudioClient client, int bufferMillis, CancellationToken cancelToken, int maxFrameSize = 1500) public BufferedWriteStream(AudioStream next, IAudioClient client, int bufferMillis, CancellationToken cancelToken, int maxFrameSize = 1500)
: this(next, client as AudioClient, bufferMillis, cancelToken, null, maxFrameSize) { } : this(next, client as AudioClient, bufferMillis, cancelToken, null, maxFrameSize) { }
internal BufferedWriteStream(AudioStream next, AudioClient client, int bufferMillis, CancellationToken cancelToken, Logger logger, int maxFrameSize = 1500)
internal BufferedWriteStream(AudioStream next, AudioClient client, int bufferMillis, CancellationToken cancelToken, ILogger logger, int maxFrameSize = 1500)
{ {
//maxFrameSize = 1275 was too limiting at 128kbps,2ch,60ms //maxFrameSize = 1275 was too limiting at 128kbps,2ch,60ms
_next = next; _next = next;
@@ -106,7 +107,7 @@ namespace Discord.Audio.Streams
timestamp += OpusEncoder.FrameSamplesPerChannel; timestamp += OpusEncoder.FrameSamplesPerChannel;
_silenceFrames = 0; _silenceFrames = 0;
#if DEBUG #if DEBUG
var _ = _logger?.DebugAsync($"Sent {frame.Bytes} bytes ({_queuedFrames.Count} frames buffered)");
_logger?.LogDebug($"Sent {frame.Bytes} bytes ({_queuedFrames.Count} frames buffered)");
#endif #endif
} }
else else
@@ -125,7 +126,7 @@ namespace Discord.Audio.Streams
timestamp += OpusEncoder.FrameSamplesPerChannel; timestamp += OpusEncoder.FrameSamplesPerChannel;
} }
#if DEBUG #if DEBUG
var _ = _logger?.DebugAsync("Buffer underrun");
_logger?.LogDebug("Buffer underrun");
#endif #endif
} }
} }
@@ -153,7 +154,7 @@ namespace Discord.Audio.Streams
if (!_bufferPool.TryDequeue(out byte[] buffer)) if (!_bufferPool.TryDequeue(out byte[] buffer))
{ {
#if DEBUG #if DEBUG
var _ = _logger?.DebugAsync("Buffer overflow"); //Should never happen because of the queueLock
_logger?.LogDebug("Buffer overflow"); //Should never happen because of the queueLock
#endif #endif
#pragma warning disable IDISP016 #pragma warning disable IDISP016
writeCancelToken?.Dispose(); writeCancelToken?.Dispose();
@@ -165,7 +166,7 @@ namespace Discord.Audio.Streams
if (!_isPreloaded && _queuedFrames.Count == _queueLength) if (!_isPreloaded && _queuedFrames.Count == _queueLength)
{ {
#if DEBUG #if DEBUG
var _ = _logger?.DebugAsync("Preloaded");
_logger?.LogDebug("Preloaded");
#endif #endif
_isPreloaded = true; _isPreloaded = true;
} }


+ 10
- 9
src/Discord.Net.WebSocket/ConnectionManager.cs View File

@@ -3,6 +3,7 @@ using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.Net; using Discord.Net;
using Microsoft.Extensions.Logging;


namespace Discord namespace Discord
{ {
@@ -14,7 +15,7 @@ namespace Discord
private readonly AsyncEvent<Func<Exception, bool, Task>> _disconnectedEvent = new AsyncEvent<Func<Exception, bool, Task>>(); private readonly AsyncEvent<Func<Exception, bool, Task>> _disconnectedEvent = new AsyncEvent<Func<Exception, bool, Task>>();


private readonly SemaphoreSlim _stateLock; private readonly SemaphoreSlim _stateLock;
private readonly Logger _logger;
private readonly ILogger _logger;
private readonly int _connectionTimeout; private readonly int _connectionTimeout;
private readonly Func<Task> _onConnecting; private readonly Func<Task> _onConnecting;
private readonly Func<Exception, Task> _onDisconnecting; private readonly Func<Exception, Task> _onDisconnecting;
@@ -28,7 +29,7 @@ namespace Discord
public ConnectionState State { get; private set; } public ConnectionState State { get; private set; }
public CancellationToken CancelToken { get; private set; } public CancellationToken CancelToken { get; private set; }


internal ConnectionManager(SemaphoreSlim stateLock, Logger logger, int connectionTimeout,
internal ConnectionManager(SemaphoreSlim stateLock, ILogger logger, int connectionTimeout,
Func<Task> onConnecting, Func<Exception, Task> onDisconnecting, Action<Func<Exception, Task>> clientDisconnectHandler) Func<Task> onConnecting, Func<Exception, Task> onDisconnecting, Action<Func<Exception, Task>> clientDisconnectHandler)
{ {
_stateLock = stateLock; _stateLock = stateLock;
@@ -85,12 +86,12 @@ namespace Discord
Error(ex); //In case this exception didn't come from another Error call Error(ex); //In case this exception didn't come from another Error call
if (!reconnectCancelToken.IsCancellationRequested) if (!reconnectCancelToken.IsCancellationRequested)
{ {
await _logger.WarningAsync(ex).ConfigureAwait(false);
_logger.LogWarning(ex, ex.Message);
await DisconnectAsync(ex, true).ConfigureAwait(false); await DisconnectAsync(ex, true).ConfigureAwait(false);
} }
else else
{ {
await _logger.ErrorAsync(ex).ConfigureAwait(false);
_logger.LogError(ex, ex.Message);
await DisconnectAsync(ex, false).ConfigureAwait(false); await DisconnectAsync(ex, false).ConfigureAwait(false);
} }
} }
@@ -124,7 +125,7 @@ namespace Discord


_connectionPromise = new TaskCompletionSource<bool>(); _connectionPromise = new TaskCompletionSource<bool>();
State = ConnectionState.Connecting; State = ConnectionState.Connecting;
await _logger.InfoAsync("Connecting").ConfigureAwait(false);
_logger.LogInformation("Connecting");


try try
{ {
@@ -154,9 +155,9 @@ namespace Discord
throw innerEx; throw innerEx;
} }


await _logger.InfoAsync("Connected").ConfigureAwait(false);
_logger.LogInformation("Connected");
State = ConnectionState.Connected; State = ConnectionState.Connected;
await _logger.DebugAsync("Raising Event").ConfigureAwait(false);
_logger.LogDebug("Raising Event");
await _connectedEvent.InvokeAsync().ConfigureAwait(false); await _connectedEvent.InvokeAsync().ConfigureAwait(false);
} }
catch (Exception ex) catch (Exception ex)
@@ -169,13 +170,13 @@ namespace Discord
{ {
if (State == ConnectionState.Disconnected) return; if (State == ConnectionState.Disconnected) return;
State = ConnectionState.Disconnecting; State = ConnectionState.Disconnecting;
await _logger.InfoAsync("Disconnecting").ConfigureAwait(false);
_logger.LogInformation("Disconnecting");


await _onDisconnecting(ex).ConfigureAwait(false); await _onDisconnecting(ex).ConfigureAwait(false);


await _disconnectedEvent.InvokeAsync(ex, isReconnecting).ConfigureAwait(false); await _disconnectedEvent.InvokeAsync(ex, isReconnecting).ConfigureAwait(false);
State = ConnectionState.Disconnected; State = ConnectionState.Disconnected;
await _logger.InfoAsync("Disconnected").ConfigureAwait(false);
_logger.LogInformation("Disconnected");
} }


public async Task CompleteAsync() public async Task CompleteAsync()


+ 0
- 1
src/Discord.Net.WebSocket/DiscordShardedClient.cs View File

@@ -328,7 +328,6 @@ namespace Discord.WebSocket


private void RegisterEvents(DiscordSocketClient client, bool isPrimary) private void RegisterEvents(DiscordSocketClient client, bool isPrimary)
{ {
client.Log += (msg) => _logEvent.InvokeAsync(msg);
client.LoggedOut += () => client.LoggedOut += () =>
{ {
var state = LoginState; var state = LoginState;


+ 137
- 103
src/Discord.Net.WebSocket/DiscordSocketClient.cs View File

@@ -5,6 +5,7 @@ using Discord.Net.Converters;
using Discord.Net.Udp; using Discord.Net.Udp;
using Discord.Net.WebSockets; using Discord.Net.WebSockets;
using Discord.Rest; using Discord.Rest;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System; using System;
@@ -30,7 +31,7 @@ namespace Discord.WebSocket
private readonly DiscordSocketClient _parentClient; private readonly DiscordSocketClient _parentClient;
private readonly ConcurrentQueue<long> _heartbeatTimes; private readonly ConcurrentQueue<long> _heartbeatTimes;
private readonly ConnectionManager _connection; private readonly ConnectionManager _connection;
private readonly Logger _gatewayLogger;
private readonly ILogger _gatewayLogger;
private readonly SemaphoreSlim _stateLock; private readonly SemaphoreSlim _stateLock;


private string _sessionId; private string _sessionId;
@@ -144,6 +145,7 @@ namespace Discord.WebSocket
_gatewayIntents = config.GatewayIntents; _gatewayIntents = config.GatewayIntents;


_stateLock = new SemaphoreSlim(1, 1); _stateLock = new SemaphoreSlim(1, 1);

_gatewayLogger = LogManager.CreateLogger(ShardId == 0 && TotalShards == 1 ? "Gateway" : $"Shard #{ShardId}"); _gatewayLogger = LogManager.CreateLogger(ShardId == 0 && TotalShards == 1 ? "Gateway" : $"Shard #{ShardId}");
_connection = new ConnectionManager(_stateLock, _gatewayLogger, config.ConnectionTimeout, _connection = new ConnectionManager(_stateLock, _gatewayLogger, config.ConnectionTimeout,
OnConnectingAsync, OnDisconnectingAsync, x => ApiClient.Disconnected += x); OnConnectingAsync, OnDisconnectingAsync, x => ApiClient.Disconnected += x);
@@ -157,18 +159,42 @@ namespace Discord.WebSocket
_serializer = new JsonSerializer { ContractResolver = new DiscordContractResolver() }; _serializer = new JsonSerializer { ContractResolver = new DiscordContractResolver() };
_serializer.Error += (s, e) => _serializer.Error += (s, e) =>
{ {
_gatewayLogger.WarningAsync("Serializer Error", e.ErrorContext.Error).GetAwaiter().GetResult();
_gatewayLogger.LogWarning(e.ErrorContext.Error, "Serializer Error");
e.ErrorContext.Handled = true; e.ErrorContext.Handled = true;
}; };


ApiClient.SentGatewayMessage += async opCode => await _gatewayLogger.DebugAsync($"Sent {opCode}").ConfigureAwait(false);
ApiClient.SentGatewayMessage += opCode =>
{
_gatewayLogger.LogDebug($"Sent {opCode}");
return Task.CompletedTask;
};
ApiClient.ReceivedGatewayEvent += ProcessMessageAsync; ApiClient.ReceivedGatewayEvent += ProcessMessageAsync;


LeftGuild += async g => await _gatewayLogger.InfoAsync($"Left {g.Name}").ConfigureAwait(false);
JoinedGuild += async g => await _gatewayLogger.InfoAsync($"Joined {g.Name}").ConfigureAwait(false);
GuildAvailable += async g => await _gatewayLogger.VerboseAsync($"Connected to {g.Name}").ConfigureAwait(false);
GuildUnavailable += async g => await _gatewayLogger.VerboseAsync($"Disconnected from {g.Name}").ConfigureAwait(false);
LatencyUpdated += async (old, val) => await _gatewayLogger.DebugAsync($"Latency = {val} ms").ConfigureAwait(false);
LeftGuild += g =>
{
_gatewayLogger.LogInformation($"Left {g.Name}");
return Task.CompletedTask;
};
JoinedGuild += g =>
{
_gatewayLogger.LogInformation($"Joined {g.Name}");
return Task.CompletedTask;
};
GuildAvailable += g =>
{
_gatewayLogger.LogTrace($"Connected to {g.Name}");
return Task.CompletedTask;
};
GuildUnavailable += g =>
{
_gatewayLogger.LogTrace($"Disconnected from {g.Name}");
return Task.CompletedTask;
};
LatencyUpdated += (old, val) =>
{
_gatewayLogger.LogDebug($"Latency = {val} ms");
return Task.CompletedTask;
};


GuildAvailable += g => GuildAvailable += g =>
{ {
@@ -232,17 +258,17 @@ namespace Discord.WebSocket
} }
try try
{ {
await _gatewayLogger.DebugAsync("Connecting ApiClient").ConfigureAwait(false);
_gatewayLogger.LogDebug("Connecting ApiClient");
await ApiClient.ConnectAsync().ConfigureAwait(false); await ApiClient.ConnectAsync().ConfigureAwait(false);


if (_sessionId != null) if (_sessionId != null)
{ {
await _gatewayLogger.DebugAsync("Resuming").ConfigureAwait(false);
_gatewayLogger.LogDebug("Resuming");
await ApiClient.SendResumeAsync(_sessionId, _lastSeq).ConfigureAwait(false); await ApiClient.SendResumeAsync(_sessionId, _lastSeq).ConfigureAwait(false);
} }
else else
{ {
await _gatewayLogger.DebugAsync("Identifying").ConfigureAwait(false);
_gatewayLogger.LogDebug("Identifying");
await ApiClient.SendIdentifyAsync(shardID: ShardId, totalShards: TotalShards, guildSubscriptions: _guildSubscriptions, gatewayIntents: _gatewayIntents, presence: BuildCurrentStatus()).ConfigureAwait(false); await ApiClient.SendIdentifyAsync(shardID: ShardId, totalShards: TotalShards, guildSubscriptions: _guildSubscriptions, gatewayIntents: _gatewayIntents, presence: BuildCurrentStatus()).ConfigureAwait(false);
} }


@@ -258,11 +284,11 @@ namespace Discord.WebSocket
private async Task OnDisconnectingAsync(Exception ex) private async Task OnDisconnectingAsync(Exception ex)
{ {


await _gatewayLogger.DebugAsync("Disconnecting ApiClient").ConfigureAwait(false);
_gatewayLogger.LogDebug("Disconnecting ApiClient");
await ApiClient.DisconnectAsync(ex).ConfigureAwait(false); await ApiClient.DisconnectAsync(ex).ConfigureAwait(false);


//Wait for tasks to complete //Wait for tasks to complete
await _gatewayLogger.DebugAsync("Waiting for heartbeater").ConfigureAwait(false);
_gatewayLogger.LogDebug("Waiting for heartbeater");
var heartbeatTask = _heartbeatTask; var heartbeatTask = _heartbeatTask;
if (heartbeatTask != null) if (heartbeatTask != null)
await heartbeatTask.ConfigureAwait(false); await heartbeatTask.ConfigureAwait(false);
@@ -271,18 +297,18 @@ namespace Discord.WebSocket
while (_heartbeatTimes.TryDequeue(out _)) { } while (_heartbeatTimes.TryDequeue(out _)) { }
_lastMessageTime = 0; _lastMessageTime = 0;


await _gatewayLogger.DebugAsync("Waiting for guild downloader").ConfigureAwait(false);
_gatewayLogger.LogDebug("Waiting for guild downloader");
var guildDownloadTask = _guildDownloadTask; var guildDownloadTask = _guildDownloadTask;
if (guildDownloadTask != null) if (guildDownloadTask != null)
await guildDownloadTask.ConfigureAwait(false); await guildDownloadTask.ConfigureAwait(false);
_guildDownloadTask = null; _guildDownloadTask = null;


//Clear large guild queue //Clear large guild queue
await _gatewayLogger.DebugAsync("Clearing large guild queue").ConfigureAwait(false);
_gatewayLogger.LogDebug("Clearing large guild queue");
while (_largeGuilds.TryDequeue(out _)) { } while (_largeGuilds.TryDequeue(out _)) { }


//Raise virtual GUILD_UNAVAILABLEs //Raise virtual GUILD_UNAVAILABLEs
await _gatewayLogger.DebugAsync("Raising virtual GuildUnavailables").ConfigureAwait(false);
_gatewayLogger.LogDebug("Raising virtual GuildUnavailables");
foreach (var guild in State.Guilds) foreach (var guild in State.Guilds)
{ {
if (guild.IsAvailable) if (guild.IsAvailable)
@@ -524,7 +550,7 @@ namespace Discord.WebSocket
{ {
case GatewayOpCode.Hello: case GatewayOpCode.Hello:
{ {
await _gatewayLogger.DebugAsync("Received Hello").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Hello");
var data = (payload as JToken).ToObject<HelloEvent>(_serializer); var data = (payload as JToken).ToObject<HelloEvent>(_serializer);


_heartbeatTask = RunHeartbeatAsync(data.HeartbeatInterval, _connection.CancelToken); _heartbeatTask = RunHeartbeatAsync(data.HeartbeatInterval, _connection.CancelToken);
@@ -532,14 +558,14 @@ namespace Discord.WebSocket
break; break;
case GatewayOpCode.Heartbeat: case GatewayOpCode.Heartbeat:
{ {
await _gatewayLogger.DebugAsync("Received Heartbeat").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Heartbeat");


await ApiClient.SendHeartbeatAsync(_lastSeq).ConfigureAwait(false); await ApiClient.SendHeartbeatAsync(_lastSeq).ConfigureAwait(false);
} }
break; break;
case GatewayOpCode.HeartbeatAck: case GatewayOpCode.HeartbeatAck:
{ {
await _gatewayLogger.DebugAsync("Received HeartbeatAck").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received HeartbeatAck");


if (_heartbeatTimes.TryDequeue(out long time)) if (_heartbeatTimes.TryDequeue(out long time))
{ {
@@ -553,8 +579,8 @@ namespace Discord.WebSocket
break; break;
case GatewayOpCode.InvalidSession: case GatewayOpCode.InvalidSession:
{ {
await _gatewayLogger.DebugAsync("Received InvalidSession").ConfigureAwait(false);
await _gatewayLogger.WarningAsync("Failed to resume previous session").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received InvalidSession");
_gatewayLogger.LogWarning("Failed to resume previous session");


_sessionId = null; _sessionId = null;
_lastSeq = 0; _lastSeq = 0;
@@ -577,7 +603,7 @@ namespace Discord.WebSocket
break; break;
case GatewayOpCode.Reconnect: case GatewayOpCode.Reconnect:
{ {
await _gatewayLogger.DebugAsync("Received Reconnect").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Reconnect");
_connection.Error(new GatewayReconnectException("Server requested a reconnect")); _connection.Error(new GatewayReconnectException("Server requested a reconnect"));
} }
break; break;
@@ -589,7 +615,7 @@ namespace Discord.WebSocket
{ {
try try
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (READY)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (READY)");


var data = (payload as JToken).ToObject<ReadyEvent>(_serializer); var data = (payload as JToken).ToObject<ReadyEvent>(_serializer);
var state = new ClientState(data.Guilds.Length, data.PrivateChannels.Length); var state = new ClientState(data.Guilds.Length, data.PrivateChannels.Length);
@@ -637,14 +663,14 @@ namespace Discord.WebSocket
_ = DownloadUsersAsync(Guilds.Where(x => x.IsAvailable && !x.HasAllMembers)); _ = DownloadUsersAsync(Guilds.Where(x => x.IsAvailable && !x.HasAllMembers));


await TimedInvokeAsync(_readyEvent, nameof(Ready)).ConfigureAwait(false); await TimedInvokeAsync(_readyEvent, nameof(Ready)).ConfigureAwait(false);
await _gatewayLogger.InfoAsync("Ready").ConfigureAwait(false);
_gatewayLogger.LogInformation("Ready");
}); });
_ = _connection.CompleteAsync(); _ = _connection.CompleteAsync();
} }
break; break;
case "RESUMED": case "RESUMED":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (RESUMED)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (RESUMED)");


_ = _connection.CompleteAsync(); _ = _connection.CompleteAsync();


@@ -655,7 +681,7 @@ namespace Discord.WebSocket
await GuildAvailableAsync(guild).ConfigureAwait(false); await GuildAvailableAsync(guild).ConfigureAwait(false);
} }


await _gatewayLogger.InfoAsync("Resumed previous session").ConfigureAwait(false);
_gatewayLogger.LogInformation("Resumed previous session");
} }
break; break;


@@ -668,7 +694,7 @@ namespace Discord.WebSocket
{ {
type = "GUILD_AVAILABLE"; type = "GUILD_AVAILABLE";
_lastGuildAvailableTime = Environment.TickCount; _lastGuildAvailableTime = Environment.TickCount;
await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_AVAILABLE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (GUILD_AVAILABLE)");


var guild = State.GetGuild(data.Id); var guild = State.GetGuild(data.Id);
if (guild != null) if (guild != null)
@@ -693,7 +719,7 @@ namespace Discord.WebSocket
} }
else else
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_CREATE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (GUILD_CREATE)");


var guild = AddGuild(data, State); var guild = AddGuild(data, State);
if (guild != null) if (guild != null)
@@ -711,7 +737,7 @@ namespace Discord.WebSocket
break; break;
case "GUILD_UPDATE": case "GUILD_UPDATE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_UPDATE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (GUILD_UPDATE)");


var data = (payload as JToken).ToObject<API.Guild>(_serializer); var data = (payload as JToken).ToObject<API.Guild>(_serializer);
var guild = State.GetGuild(data.Id); var guild = State.GetGuild(data.Id);
@@ -730,7 +756,7 @@ namespace Discord.WebSocket
break; break;
case "GUILD_EMOJIS_UPDATE": case "GUILD_EMOJIS_UPDATE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_EMOJIS_UPDATE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (GUILD_EMOJIS_UPDATE)");


var data = (payload as JToken).ToObject<API.Gateway.GuildEmojiUpdateEvent>(_serializer); var data = (payload as JToken).ToObject<API.Gateway.GuildEmojiUpdateEvent>(_serializer);
var guild = State.GetGuild(data.GuildId); var guild = State.GetGuild(data.GuildId);
@@ -749,7 +775,7 @@ namespace Discord.WebSocket
break; break;
case "GUILD_SYNC": case "GUILD_SYNC":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_SYNC)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (GUILD_SYNC)");
var data = (payload as JToken).ToObject<GuildSyncEvent>(_serializer); var data = (payload as JToken).ToObject<GuildSyncEvent>(_serializer);
var guild = State.GetGuild(data.Id); var guild = State.GetGuild(data.Id);
if (guild != null) if (guild != null)
@@ -775,7 +801,7 @@ namespace Discord.WebSocket
if (data.Unavailable == true) if (data.Unavailable == true)
{ {
type = "GUILD_UNAVAILABLE"; type = "GUILD_UNAVAILABLE";
await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_UNAVAILABLE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (GUILD_UNAVAILABLE)");


var guild = State.GetGuild(data.Id); var guild = State.GetGuild(data.Id);
if (guild != null) if (guild != null)
@@ -791,7 +817,7 @@ namespace Discord.WebSocket
} }
else else
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_DELETE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (GUILD_DELETE)");


var guild = RemoveGuild(data.Id); var guild = RemoveGuild(data.Id);
if (guild != null) if (guild != null)
@@ -812,7 +838,7 @@ namespace Discord.WebSocket
//Channels //Channels
case "CHANNEL_CREATE": case "CHANNEL_CREATE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (CHANNEL_CREATE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (CHANNEL_CREATE)");


var data = (payload as JToken).ToObject<API.Channel>(_serializer); var data = (payload as JToken).ToObject<API.Channel>(_serializer);
SocketChannel channel = null; SocketChannel channel = null;
@@ -849,7 +875,7 @@ namespace Discord.WebSocket
break; break;
case "CHANNEL_UPDATE": case "CHANNEL_UPDATE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (CHANNEL_UPDATE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (CHANNEL_UPDATE)");


var data = (payload as JToken).ToObject<API.Channel>(_serializer); var data = (payload as JToken).ToObject<API.Channel>(_serializer);
var channel = State.GetChannel(data.Id); var channel = State.GetChannel(data.Id);
@@ -876,7 +902,7 @@ namespace Discord.WebSocket
break; break;
case "CHANNEL_DELETE": case "CHANNEL_DELETE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (CHANNEL_DELETE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (CHANNEL_DELETE)");


SocketChannel channel = null; SocketChannel channel = null;
var data = (payload as JToken).ToObject<API.Channel>(_serializer); var data = (payload as JToken).ToObject<API.Channel>(_serializer);
@@ -915,7 +941,7 @@ namespace Discord.WebSocket
//Members //Members
case "GUILD_MEMBER_ADD": case "GUILD_MEMBER_ADD":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_MEMBER_ADD)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (GUILD_MEMBER_ADD)");


var data = (payload as JToken).ToObject<GuildMemberAddEvent>(_serializer); var data = (payload as JToken).ToObject<GuildMemberAddEvent>(_serializer);
var guild = State.GetGuild(data.GuildId); var guild = State.GetGuild(data.GuildId);
@@ -941,7 +967,7 @@ namespace Discord.WebSocket
break; break;
case "GUILD_MEMBER_UPDATE": case "GUILD_MEMBER_UPDATE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_MEMBER_UPDATE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (GUILD_MEMBER_UPDATE)");


var data = (payload as JToken).ToObject<GuildMemberUpdateEvent>(_serializer); var data = (payload as JToken).ToObject<GuildMemberUpdateEvent>(_serializer);
var guild = State.GetGuild(data.GuildId); var guild = State.GetGuild(data.GuildId);
@@ -986,7 +1012,7 @@ namespace Discord.WebSocket
break; break;
case "GUILD_MEMBER_REMOVE": case "GUILD_MEMBER_REMOVE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_MEMBER_REMOVE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (GUILD_MEMBER_REMOVE)");


var data = (payload as JToken).ToObject<GuildMemberRemoveEvent>(_serializer); var data = (payload as JToken).ToObject<GuildMemberRemoveEvent>(_serializer);
var guild = State.GetGuild(data.GuildId); var guild = State.GetGuild(data.GuildId);
@@ -1021,7 +1047,7 @@ namespace Discord.WebSocket
break; break;
case "GUILD_MEMBERS_CHUNK": case "GUILD_MEMBERS_CHUNK":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_MEMBERS_CHUNK)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (GUILD_MEMBERS_CHUNK)");


var data = (payload as JToken).ToObject<GuildMembersChunkEvent>(_serializer); var data = (payload as JToken).ToObject<GuildMembersChunkEvent>(_serializer);
var guild = State.GetGuild(data.GuildId); var guild = State.GetGuild(data.GuildId);
@@ -1045,7 +1071,7 @@ namespace Discord.WebSocket
break; break;
case "CHANNEL_RECIPIENT_ADD": case "CHANNEL_RECIPIENT_ADD":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (CHANNEL_RECIPIENT_ADD)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (CHANNEL_RECIPIENT_ADD)");


var data = (payload as JToken).ToObject<RecipientEvent>(_serializer); var data = (payload as JToken).ToObject<RecipientEvent>(_serializer);
if (State.GetChannel(data.ChannelId) is SocketGroupChannel channel) if (State.GetChannel(data.ChannelId) is SocketGroupChannel channel)
@@ -1062,7 +1088,7 @@ namespace Discord.WebSocket
break; break;
case "CHANNEL_RECIPIENT_REMOVE": case "CHANNEL_RECIPIENT_REMOVE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (CHANNEL_RECIPIENT_REMOVE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (CHANNEL_RECIPIENT_REMOVE)");


var data = (payload as JToken).ToObject<RecipientEvent>(_serializer); var data = (payload as JToken).ToObject<RecipientEvent>(_serializer);
if (State.GetChannel(data.ChannelId) is SocketGroupChannel channel) if (State.GetChannel(data.ChannelId) is SocketGroupChannel channel)
@@ -1087,7 +1113,7 @@ namespace Discord.WebSocket
//Roles //Roles
case "GUILD_ROLE_CREATE": case "GUILD_ROLE_CREATE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_ROLE_CREATE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (GUILD_ROLE_CREATE)");


var data = (payload as JToken).ToObject<GuildRoleCreateEvent>(_serializer); var data = (payload as JToken).ToObject<GuildRoleCreateEvent>(_serializer);
var guild = State.GetGuild(data.GuildId); var guild = State.GetGuild(data.GuildId);
@@ -1111,7 +1137,7 @@ namespace Discord.WebSocket
break; break;
case "GUILD_ROLE_UPDATE": case "GUILD_ROLE_UPDATE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_ROLE_UPDATE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (GUILD_ROLE_UPDATE)");


var data = (payload as JToken).ToObject<GuildRoleUpdateEvent>(_serializer); var data = (payload as JToken).ToObject<GuildRoleUpdateEvent>(_serializer);
var guild = State.GetGuild(data.GuildId); var guild = State.GetGuild(data.GuildId);
@@ -1146,7 +1172,7 @@ namespace Discord.WebSocket
break; break;
case "GUILD_ROLE_DELETE": case "GUILD_ROLE_DELETE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_ROLE_DELETE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (GUILD_ROLE_DELETE)");


var data = (payload as JToken).ToObject<GuildRoleDeleteEvent>(_serializer); var data = (payload as JToken).ToObject<GuildRoleDeleteEvent>(_serializer);
var guild = State.GetGuild(data.GuildId); var guild = State.GetGuild(data.GuildId);
@@ -1180,7 +1206,7 @@ namespace Discord.WebSocket
//Bans //Bans
case "GUILD_BAN_ADD": case "GUILD_BAN_ADD":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_BAN_ADD)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (GUILD_BAN_ADD)");


var data = (payload as JToken).ToObject<GuildBanEvent>(_serializer); var data = (payload as JToken).ToObject<GuildBanEvent>(_serializer);
var guild = State.GetGuild(data.GuildId); var guild = State.GetGuild(data.GuildId);
@@ -1206,7 +1232,7 @@ namespace Discord.WebSocket
break; break;
case "GUILD_BAN_REMOVE": case "GUILD_BAN_REMOVE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_BAN_REMOVE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (GUILD_BAN_REMOVE)");


var data = (payload as JToken).ToObject<GuildBanEvent>(_serializer); var data = (payload as JToken).ToObject<GuildBanEvent>(_serializer);
var guild = State.GetGuild(data.GuildId); var guild = State.GetGuild(data.GuildId);
@@ -1234,7 +1260,7 @@ namespace Discord.WebSocket
//Messages //Messages
case "MESSAGE_CREATE": case "MESSAGE_CREATE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_CREATE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (MESSAGE_CREATE)");


var data = (payload as JToken).ToObject<API.Message>(_serializer); var data = (payload as JToken).ToObject<API.Message>(_serializer);
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
@@ -1291,7 +1317,7 @@ namespace Discord.WebSocket
break; break;
case "MESSAGE_UPDATE": case "MESSAGE_UPDATE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_UPDATE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (MESSAGE_UPDATE)");


var data = (payload as JToken).ToObject<API.Message>(_serializer); var data = (payload as JToken).ToObject<API.Message>(_serializer);
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
@@ -1344,7 +1370,7 @@ namespace Discord.WebSocket
break; break;
case "MESSAGE_DELETE": case "MESSAGE_DELETE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_DELETE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (MESSAGE_DELETE)");


var data = (payload as JToken).ToObject<API.Message>(_serializer); var data = (payload as JToken).ToObject<API.Message>(_serializer);
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
@@ -1371,7 +1397,7 @@ namespace Discord.WebSocket
break; break;
case "MESSAGE_REACTION_ADD": case "MESSAGE_REACTION_ADD":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_ADD)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (MESSAGE_REACTION_ADD)");


var data = (payload as JToken).ToObject<API.Gateway.Reaction>(_serializer); var data = (payload as JToken).ToObject<API.Gateway.Reaction>(_serializer);
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
@@ -1404,7 +1430,7 @@ namespace Discord.WebSocket
break; break;
case "MESSAGE_REACTION_REMOVE": case "MESSAGE_REACTION_REMOVE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_REMOVE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (MESSAGE_REACTION_REMOVE)");


var data = (payload as JToken).ToObject<API.Gateway.Reaction>(_serializer); var data = (payload as JToken).ToObject<API.Gateway.Reaction>(_serializer);
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
@@ -1437,7 +1463,7 @@ namespace Discord.WebSocket
break; break;
case "MESSAGE_REACTION_REMOVE_ALL": case "MESSAGE_REACTION_REMOVE_ALL":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_REMOVE_ALL)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (MESSAGE_REACTION_REMOVE_ALL)");


var data = (payload as JToken).ToObject<RemoveAllReactionsEvent>(_serializer); var data = (payload as JToken).ToObject<RemoveAllReactionsEvent>(_serializer);
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
@@ -1459,7 +1485,7 @@ namespace Discord.WebSocket
break; break;
case "MESSAGE_REACTION_REMOVE_EMOJI": case "MESSAGE_REACTION_REMOVE_EMOJI":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_REMOVE_EMOJI)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (MESSAGE_REACTION_REMOVE_EMOJI)");


var data = (payload as JToken).ToObject<API.Gateway.RemoveAllReactionsForEmoteEvent>(_serializer); var data = (payload as JToken).ToObject<API.Gateway.RemoveAllReactionsForEmoteEvent>(_serializer);
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
@@ -1487,11 +1513,11 @@ namespace Discord.WebSocket
break; break;
case "MESSAGE_DELETE_BULK": case "MESSAGE_DELETE_BULK":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_DELETE_BULK)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (MESSAGE_DELETE_BULK)");


if (!ExclusiveBulkDelete.HasValue) if (!ExclusiveBulkDelete.HasValue)
{ {
await _gatewayLogger.WarningAsync("A bulk delete event has been received, but the event handling behavior has not been set. " +
_gatewayLogger.LogWarning("A bulk delete event has been received, but the event handling behavior has not been set. " +
"To suppress this message, set the ExclusiveBulkDelete configuration property. " + "To suppress this message, set the ExclusiveBulkDelete configuration property. " +
"This message will appear only once."); "This message will appear only once.");
ExclusiveBulkDelete = false; ExclusiveBulkDelete = false;
@@ -1532,7 +1558,7 @@ namespace Discord.WebSocket
//Statuses //Statuses
case "PRESENCE_UPDATE": case "PRESENCE_UPDATE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (PRESENCE_UPDATE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (PRESENCE_UPDATE)");


var data = (payload as JToken).ToObject<API.Presence>(_serializer); var data = (payload as JToken).ToObject<API.Presence>(_serializer);


@@ -1591,7 +1617,7 @@ namespace Discord.WebSocket
break; break;
case "TYPING_START": case "TYPING_START":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (TYPING_START)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (TYPING_START)");


var data = (payload as JToken).ToObject<TypingStartEvent>(_serializer); var data = (payload as JToken).ToObject<TypingStartEvent>(_serializer);
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
@@ -1618,7 +1644,7 @@ namespace Discord.WebSocket
//Users //Users
case "USER_UPDATE": case "USER_UPDATE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (USER_UPDATE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (USER_UPDATE)");


var data = (payload as JToken).ToObject<API.User>(_serializer); var data = (payload as JToken).ToObject<API.User>(_serializer);
if (data.Id == CurrentUser.Id) if (data.Id == CurrentUser.Id)
@@ -1629,7 +1655,7 @@ namespace Discord.WebSocket
} }
else else
{ {
await _gatewayLogger.WarningAsync("Received USER_UPDATE for wrong user.").ConfigureAwait(false);
_gatewayLogger.LogWarning("Received USER_UPDATE for wrong user.");
return; return;
} }
} }
@@ -1638,7 +1664,7 @@ namespace Discord.WebSocket
//Voice //Voice
case "VOICE_STATE_UPDATE": case "VOICE_STATE_UPDATE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (VOICE_STATE_UPDATE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (VOICE_STATE_UPDATE)");


var data = (payload as JToken).ToObject<API.VoiceState>(_serializer); var data = (payload as JToken).ToObject<API.VoiceState>(_serializer);
SocketUser user; SocketUser user;
@@ -1712,7 +1738,7 @@ namespace Discord.WebSocket
break; break;
case "VOICE_SERVER_UPDATE": case "VOICE_SERVER_UPDATE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (VOICE_SERVER_UPDATE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (VOICE_SERVER_UPDATE)");


var data = (payload as JToken).ToObject<VoiceServerUpdateEvent>(_serializer); var data = (payload as JToken).ToObject<VoiceServerUpdateEvent>(_serializer);
var guild = State.GetGuild(data.GuildId); var guild = State.GetGuild(data.GuildId);
@@ -1745,7 +1771,7 @@ namespace Discord.WebSocket
//Invites //Invites
case "INVITE_CREATE": case "INVITE_CREATE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (INVITE_CREATE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (INVITE_CREATE)");


var data = (payload as JToken).ToObject<API.Gateway.InviteCreateEvent>(_serializer); var data = (payload as JToken).ToObject<API.Gateway.InviteCreateEvent>(_serializer);
if (State.GetChannel(data.ChannelId) is SocketGuildChannel channel) if (State.GetChannel(data.ChannelId) is SocketGuildChannel channel)
@@ -1778,7 +1804,7 @@ namespace Discord.WebSocket
break; break;
case "INVITE_DELETE": case "INVITE_DELETE":
{ {
await _gatewayLogger.DebugAsync("Received Dispatch (INVITE_DELETE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Received Dispatch (INVITE_DELETE)");


var data = (payload as JToken).ToObject<API.Gateway.InviteDeleteEvent>(_serializer); var data = (payload as JToken).ToObject<API.Gateway.InviteDeleteEvent>(_serializer);
if (State.GetChannel(data.ChannelId) is SocketGuildChannel channel) if (State.GetChannel(data.ChannelId) is SocketGuildChannel channel)
@@ -1802,41 +1828,41 @@ namespace Discord.WebSocket


//Ignored (User only) //Ignored (User only)
case "CHANNEL_PINS_ACK": case "CHANNEL_PINS_ACK":
await _gatewayLogger.DebugAsync("Ignored Dispatch (CHANNEL_PINS_ACK)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Ignored Dispatch (CHANNEL_PINS_ACK)");
break; break;
case "CHANNEL_PINS_UPDATE": case "CHANNEL_PINS_UPDATE":
await _gatewayLogger.DebugAsync("Ignored Dispatch (CHANNEL_PINS_UPDATE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Ignored Dispatch (CHANNEL_PINS_UPDATE)");
break; break;
case "GUILD_INTEGRATIONS_UPDATE": case "GUILD_INTEGRATIONS_UPDATE":
await _gatewayLogger.DebugAsync("Ignored Dispatch (GUILD_INTEGRATIONS_UPDATE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Ignored Dispatch (GUILD_INTEGRATIONS_UPDATE)");
break; break;
case "MESSAGE_ACK": case "MESSAGE_ACK":
await _gatewayLogger.DebugAsync("Ignored Dispatch (MESSAGE_ACK)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Ignored Dispatch (MESSAGE_ACK)");
break; break;
case "PRESENCES_REPLACE": case "PRESENCES_REPLACE":
await _gatewayLogger.DebugAsync("Ignored Dispatch (PRESENCES_REPLACE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Ignored Dispatch (PRESENCES_REPLACE)");
break; break;
case "USER_SETTINGS_UPDATE": case "USER_SETTINGS_UPDATE":
await _gatewayLogger.DebugAsync("Ignored Dispatch (USER_SETTINGS_UPDATE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Ignored Dispatch (USER_SETTINGS_UPDATE)");
break; break;
case "WEBHOOKS_UPDATE": case "WEBHOOKS_UPDATE":
await _gatewayLogger.DebugAsync("Ignored Dispatch (WEBHOOKS_UPDATE)").ConfigureAwait(false);
_gatewayLogger.LogDebug("Ignored Dispatch (WEBHOOKS_UPDATE)");
break; break;


//Others //Others
default: default:
await _gatewayLogger.WarningAsync($"Unknown Dispatch ({type})").ConfigureAwait(false);
_gatewayLogger.LogWarning($"Unknown Dispatch ({type})");
break; break;
} }
break; break;
default: default:
await _gatewayLogger.WarningAsync($"Unknown OpCode ({opCode})").ConfigureAwait(false);
_gatewayLogger.LogWarning($"Unknown OpCode ({opCode})");
break; break;
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
await _gatewayLogger.ErrorAsync($"Error handling {opCode}{(type != null ? $" ({type})" : "")}", ex).ConfigureAwait(false);
_gatewayLogger.LogError(ex, $"Error handling {opCode}{(type != null ? $" ({type})" : "")}");
} }
} }


@@ -1844,7 +1870,7 @@ namespace Discord.WebSocket
{ {
try try
{ {
await _gatewayLogger.DebugAsync("Heartbeat Started").ConfigureAwait(false);
_gatewayLogger.LogDebug("Heartbeat Started");
while (!cancelToken.IsCancellationRequested) while (!cancelToken.IsCancellationRequested)
{ {
int now = Environment.TickCount; int now = Environment.TickCount;
@@ -1866,20 +1892,20 @@ namespace Discord.WebSocket
} }
catch (Exception ex) catch (Exception ex)
{ {
await _gatewayLogger.WarningAsync("Heartbeat Errored", ex).ConfigureAwait(false);
_gatewayLogger.LogWarning("Heartbeat Errored", ex);
} }


await Task.Delay(intervalMillis, cancelToken).ConfigureAwait(false); await Task.Delay(intervalMillis, cancelToken).ConfigureAwait(false);
} }
await _gatewayLogger.DebugAsync("Heartbeat Stopped").ConfigureAwait(false);
_gatewayLogger.LogDebug("Heartbeat Stopped");
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
await _gatewayLogger.DebugAsync("Heartbeat Stopped").ConfigureAwait(false);
_gatewayLogger.LogDebug("Heartbeat Stopped");
} }
catch (Exception ex) catch (Exception ex)
{ {
await _gatewayLogger.ErrorAsync("Heartbeat Errored", ex).ConfigureAwait(false);
_gatewayLogger.LogError(ex, "Heartbeat Errored");
} }
} }
/*public async Task WaitForGuildsAsync() /*public async Task WaitForGuildsAsync()
@@ -1888,23 +1914,23 @@ namespace Discord.WebSocket
if (downloadTask != null) if (downloadTask != null)
await _guildDownloadTask.ConfigureAwait(false); await _guildDownloadTask.ConfigureAwait(false);
}*/ }*/
private async Task WaitForGuildsAsync(CancellationToken cancelToken, Logger logger)
private async Task WaitForGuildsAsync(CancellationToken cancelToken, ILogger logger)
{ {
//Wait for GUILD_AVAILABLEs //Wait for GUILD_AVAILABLEs
try try
{ {
await logger.DebugAsync("GuildDownloader Started").ConfigureAwait(false);
logger.LogDebug("GuildDownloader Started");
while ((_unavailableGuildCount != 0) && (Environment.TickCount - _lastGuildAvailableTime < BaseConfig.MaxWaitBetweenGuildAvailablesBeforeReady)) while ((_unavailableGuildCount != 0) && (Environment.TickCount - _lastGuildAvailableTime < BaseConfig.MaxWaitBetweenGuildAvailablesBeforeReady))
await Task.Delay(500, cancelToken).ConfigureAwait(false); await Task.Delay(500, cancelToken).ConfigureAwait(false);
await logger.DebugAsync("GuildDownloader Stopped").ConfigureAwait(false);
logger.LogDebug("GuildDownloader Stopped");
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
await logger.DebugAsync("GuildDownloader Stopped").ConfigureAwait(false);
logger.LogDebug("GuildDownloader Stopped");
} }
catch (Exception ex) catch (Exception ex)
{ {
await logger.ErrorAsync("GuildDownloader Errored", ex).ConfigureAwait(false);
logger.LogError(ex, "GuildDownloader Errored");
} }
} }
private async Task SyncGuildsAsync() private async Task SyncGuildsAsync()
@@ -2034,40 +2060,45 @@ namespace Discord.WebSocket
var handlersTask = action(); var handlersTask = action();
if (await Task.WhenAny(timeoutTask, handlersTask).ConfigureAwait(false) == timeoutTask) if (await Task.WhenAny(timeoutTask, handlersTask).ConfigureAwait(false) == timeoutTask)
{ {
await _gatewayLogger.WarningAsync($"A {name} handler is blocking the gateway task.").ConfigureAwait(false);
_gatewayLogger.LogWarning($"A {name} handler is blocking the gateway task.");
} }
await handlersTask.ConfigureAwait(false); //Ensure the handler completes await handlersTask.ConfigureAwait(false); //Ensure the handler completes
} }
catch (Exception ex) catch (Exception ex)
{ {
await _gatewayLogger.WarningAsync($"A {name} handler has thrown an unhandled exception.", ex).ConfigureAwait(false);
_gatewayLogger.LogWarning(ex, $"A {name} handler has thrown an unhandled exception.");
} }
} }


private async Task UnknownGlobalUserAsync(string evnt, ulong userId)
private Task UnknownGlobalUserAsync(string evnt, ulong userId)
{ {
string details = $"{evnt} User={userId}"; string details = $"{evnt} User={userId}";
await _gatewayLogger.WarningAsync($"Unknown User ({details}).").ConfigureAwait(false);
_gatewayLogger.LogWarning($"Unknown User ({details}).");
return Task.CompletedTask;
} }
private async Task UnknownChannelUserAsync(string evnt, ulong userId, ulong channelId)
private Task UnknownChannelUserAsync(string evnt, ulong userId, ulong channelId)
{ {
string details = $"{evnt} User={userId} Channel={channelId}"; string details = $"{evnt} User={userId} Channel={channelId}";
await _gatewayLogger.WarningAsync($"Unknown User ({details}).").ConfigureAwait(false);
_gatewayLogger.LogWarning($"Unknown User ({details}).");
return Task.CompletedTask;
} }
private async Task UnknownGuildUserAsync(string evnt, ulong userId, ulong guildId)
private Task UnknownGuildUserAsync(string evnt, ulong userId, ulong guildId)
{ {
string details = $"{evnt} User={userId} Guild={guildId}"; string details = $"{evnt} User={userId} Guild={guildId}";
await _gatewayLogger.WarningAsync($"Unknown User ({details}).").ConfigureAwait(false);
_gatewayLogger.LogWarning($"Unknown User ({details}).");
return Task.CompletedTask;
} }
private async Task IncompleteGuildUserAsync(string evnt, ulong userId, ulong guildId)
private Task IncompleteGuildUserAsync(string evnt, ulong userId, ulong guildId)
{ {
string details = $"{evnt} User={userId} Guild={guildId}"; string details = $"{evnt} User={userId} Guild={guildId}";
await _gatewayLogger.DebugAsync($"User has not been downloaded ({details}).").ConfigureAwait(false);
_gatewayLogger.LogDebug($"User has not been downloaded ({details}).");
return Task.CompletedTask;
} }
private async Task UnknownChannelAsync(string evnt, ulong channelId)
private Task UnknownChannelAsync(string evnt, ulong channelId)
{ {
string details = $"{evnt} Channel={channelId}"; string details = $"{evnt} Channel={channelId}";
await _gatewayLogger.WarningAsync($"Unknown Channel ({details}).").ConfigureAwait(false);
_gatewayLogger.LogWarning($"Unknown Channel ({details}).");
return Task.CompletedTask;
} }
private async Task UnknownChannelAsync(string evnt, ulong channelId, ulong guildId) private async Task UnknownChannelAsync(string evnt, ulong channelId, ulong guildId)
{ {
@@ -2077,22 +2108,25 @@ namespace Discord.WebSocket
return; return;
} }
string details = $"{evnt} Channel={channelId} Guild={guildId}"; string details = $"{evnt} Channel={channelId} Guild={guildId}";
await _gatewayLogger.WarningAsync($"Unknown Channel ({details}).").ConfigureAwait(false);
_gatewayLogger.LogWarning($"Unknown Channel ({details}).");
} }
private async Task UnknownRoleAsync(string evnt, ulong roleId, ulong guildId)
private Task UnknownRoleAsync(string evnt, ulong roleId, ulong guildId)
{ {
string details = $"{evnt} Role={roleId} Guild={guildId}"; string details = $"{evnt} Role={roleId} Guild={guildId}";
await _gatewayLogger.WarningAsync($"Unknown Role ({details}).").ConfigureAwait(false);
_gatewayLogger.LogWarning($"Unknown Role ({details}).");
return Task.CompletedTask;
} }
private async Task UnknownGuildAsync(string evnt, ulong guildId)
private Task UnknownGuildAsync(string evnt, ulong guildId)
{ {
string details = $"{evnt} Guild={guildId}"; string details = $"{evnt} Guild={guildId}";
await _gatewayLogger.WarningAsync($"Unknown Guild ({details}).").ConfigureAwait(false);
_gatewayLogger.LogWarning($"Unknown Guild ({details}).");
return Task.CompletedTask;
} }
private async Task UnsyncedGuildAsync(string evnt, ulong guildId)
private Task UnsyncedGuildAsync(string evnt, ulong guildId)
{ {
string details = $"{evnt} Guild={guildId}"; string details = $"{evnt} Guild={guildId}";
await _gatewayLogger.DebugAsync($"Unsynced Guild ({details}).").ConfigureAwait(false);
_gatewayLogger.LogDebug($"Unsynced Guild ({details}).");
return Task.CompletedTask;
} }


internal int GetAudioId() => _nextAudioId++; internal int GetAudioId() => _nextAudioId++;


+ 21
- 11
src/Discord.Net.Webhook/DiscordWebhookClient.cs View File

@@ -6,21 +6,19 @@ using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.Logging; using Discord.Logging;
using Discord.Rest; using Discord.Rest;
using Microsoft.Extensions.Logging;


namespace Discord.Webhook namespace Discord.Webhook
{ {
/// <summary> A client responsible for connecting as a Webhook. </summary> /// <summary> A client responsible for connecting as a Webhook. </summary>
public class DiscordWebhookClient : IDisposable public class DiscordWebhookClient : IDisposable
{ {
public event Func<LogMessage, Task> Log { add { _logEvent.Add(value); } remove { _logEvent.Remove(value); } }
internal readonly AsyncEvent<Func<LogMessage, Task>> _logEvent = new AsyncEvent<Func<LogMessage, Task>>();

private readonly ulong _webhookId; private readonly ulong _webhookId;
internal IWebhook Webhook; internal IWebhook Webhook;
internal readonly Logger _restLogger;
internal readonly ILogger _restLogger;
internal API.DiscordRestApiClient ApiClient { get; } internal API.DiscordRestApiClient ApiClient { get; }
internal LogManager LogManager { get; }
internal ILoggerFactory LogManager { get; }


/// <summary> Creates a new Webhook Discord client. </summary> /// <summary> Creates a new Webhook Discord client. </summary>
public DiscordWebhookClient(IWebhook webhook) public DiscordWebhookClient(IWebhook webhook)
@@ -69,19 +67,31 @@ namespace Discord.Webhook
private DiscordWebhookClient(DiscordRestConfig config) private DiscordWebhookClient(DiscordRestConfig config)
{ {
ApiClient = CreateApiClient(config); ApiClient = CreateApiClient(config);
LogManager = new LogManager(config.LogLevel);
LogManager.Message += async msg => await _logEvent.InvokeAsync(msg).ConfigureAwait(false);
LogManager = config.LoggerFactory;

if (LogManager == null)
{
LogManager = new DefaultLoggerFactory();
LogManager.AddProvider(new DefaultLoggerProvider());
}


_restLogger = LogManager.CreateLogger("Rest"); _restLogger = LogManager.CreateLogger("Rest");


ApiClient.RequestQueue.RateLimitTriggered += async (id, info, endpoint) =>
ApiClient.RequestQueue.RateLimitTriggered += (id, info, endpoint) =>
{ {
if (info == null) if (info == null)
await _restLogger.VerboseAsync($"Preemptive Rate limit triggered: {endpoint} {(id.IsHashBucket ? $"(Bucket: {id.BucketHash})" : "")}").ConfigureAwait(false);
_restLogger.LogTrace($"Preemptive Rate limit triggered: {endpoint} {(id.IsHashBucket ? $"(Bucket: {id.BucketHash})" : "")}");
else else
await _restLogger.WarningAsync($"Rate limit triggered: {endpoint} {(id.IsHashBucket ? $"(Bucket: {id.BucketHash})" : "")}").ConfigureAwait(false);
_restLogger.LogWarning($"Rate limit triggered: {endpoint} {(id.IsHashBucket ? $"(Bucket: {id.BucketHash})" : "")}");

return Task.CompletedTask;
};
ApiClient.SentRequest += (method, endpoint, millis) =>
{
_restLogger.LogTrace($"{method} {endpoint}: {millis} ms");
return Task.CompletedTask;
}; };
ApiClient.SentRequest += async (method, endpoint, millis) => await _restLogger.VerboseAsync($"{method} {endpoint}: {millis} ms").ConfigureAwait(false);
} }
private static API.DiscordRestApiClient CreateApiClient(DiscordRestConfig config) private static API.DiscordRestApiClient CreateApiClient(DiscordRestConfig config)
=> new API.DiscordRestApiClient(config.RestClientProvider, DiscordRestConfig.UserAgent); => new API.DiscordRestApiClient(config.RestClientProvider, DiscordRestConfig.UserAgent);


Loading…
Cancel
Save