@@ -487,6 +487,9 @@ | |||||
<Compile Include="..\Discord.Net\Legacy.cs"> | <Compile Include="..\Discord.Net\Legacy.cs"> | ||||
<Link>Legacy.cs</Link> | <Link>Legacy.cs</Link> | ||||
</Compile> | </Compile> | ||||
<Compile Include="..\Discord.Net\Logging\ILogger.cs"> | |||||
<Link>Logging\ILogger.cs</Link> | |||||
</Compile> | |||||
<Compile Include="..\Discord.Net\Logging\Logger.cs"> | <Compile Include="..\Discord.Net\Logging\Logger.cs"> | ||||
<Link>Logging\Logger.cs</Link> | <Link>Logging\Logger.cs</Link> | ||||
</Compile> | </Compile> | ||||
@@ -0,0 +1,44 @@ | |||||
using System; | |||||
namespace Discord.Logging | |||||
{ | |||||
public interface ILogger | |||||
{ | |||||
LogSeverity Level { get; } | |||||
void Log(LogSeverity severity, string message, Exception exception = null); | |||||
#if !NET45 | |||||
void Log(LogSeverity severity, FormattableString message, Exception exception = null); | |||||
#endif | |||||
void Error(string message, Exception exception = null); | |||||
#if !NET45 | |||||
void Error(FormattableString message, Exception exception = null); | |||||
#endif | |||||
void Error(Exception exception); | |||||
void Warning(string message, Exception exception = null); | |||||
#if !NET45 | |||||
void Warning(FormattableString message, Exception exception = null); | |||||
#endif | |||||
void Warning(Exception exception); | |||||
void Info(string message, Exception exception = null); | |||||
#if !NET45 | |||||
void Info(FormattableString message, Exception exception = null); | |||||
#endif | |||||
void Info(Exception exception); | |||||
void Verbose(string message, Exception exception = null); | |||||
#if !NET45 | |||||
void Verbose(FormattableString message, Exception exception = null); | |||||
#endif | |||||
void Verbose(Exception exception); | |||||
void Debug(string message, Exception exception = null); | |||||
#if !NET45 | |||||
void Debug(FormattableString message, Exception exception = null); | |||||
#endif | |||||
void Debug(Exception exception); | |||||
} | |||||
} |
@@ -2,7 +2,7 @@ | |||||
namespace Discord.Logging | namespace Discord.Logging | ||||
{ | { | ||||
public class Logger | |||||
public class Logger : ILogger | |||||
{ | { | ||||
private readonly LogManager _manager; | private readonly LogManager _manager; | ||||
@@ -22,15 +22,15 @@ namespace Discord.Net.Rest | |||||
private readonly string _baseUrl; | private readonly string _baseUrl; | ||||
private readonly AsyncLock _rateLimitLock; | private readonly AsyncLock _rateLimitLock; | ||||
private readonly ILogger _logger; | |||||
private DateTime _rateLimitTime; | private DateTime _rateLimitTime; | ||||
internal Logger Logger { get; } | |||||
public BuiltInEngine(DiscordConfig config, string baseUrl, Logger logger) | |||||
public BuiltInEngine(DiscordConfig config, string baseUrl, ILogger logger) | |||||
{ | { | ||||
_config = config; | _config = config; | ||||
_baseUrl = baseUrl; | _baseUrl = baseUrl; | ||||
Logger = logger; | |||||
_logger = logger; | |||||
_rateLimitLock = new AsyncLock(); | _rateLimitLock = new AsyncLock(); | ||||
_client = new HttpClient(new HttpClientHandler | _client = new HttpClient(new HttpClientHandler | ||||
@@ -100,15 +100,18 @@ namespace Discord.Net.Rest | |||||
int milliseconds; | int milliseconds; | ||||
if (retryAfter != null && int.TryParse(retryAfter, out milliseconds)) | if (retryAfter != null && int.TryParse(retryAfter, out milliseconds)) | ||||
{ | { | ||||
var now = DateTime.UtcNow; | |||||
if (now >= _rateLimitTime) | |||||
if (_logger != null) | |||||
{ | { | ||||
using (await _rateLimitLock.LockAsync().ConfigureAwait(false)) | |||||
var now = DateTime.UtcNow; | |||||
if (now >= _rateLimitTime) | |||||
{ | { | ||||
if (now >= _rateLimitTime) | |||||
using (await _rateLimitLock.LockAsync().ConfigureAwait(false)) | |||||
{ | { | ||||
_rateLimitTime = now.AddMilliseconds(milliseconds); | |||||
Logger.Warning($"Rate limit hit, waiting {Math.Round(milliseconds / 1000.0f, 2)} seconds"); | |||||
if (now >= _rateLimitTime) | |||||
{ | |||||
_rateLimitTime = now.AddMilliseconds(milliseconds); | |||||
_logger.Warning($"Rate limit hit, waiting {Math.Round(milliseconds / 1000.0f, 2)} seconds"); | |||||
} | |||||
} | } | ||||
} | } | ||||
} | } | ||||
@@ -9,7 +9,7 @@ namespace Discord.Net.Rest | |||||
{ | { | ||||
private readonly ETFWriter _serializer; | private readonly ETFWriter _serializer; | ||||
public ETFRestClient(DiscordConfig config, string baseUrl, Logger logger) | |||||
public ETFRestClient(DiscordConfig config, string baseUrl, ILogger logger = null) | |||||
: base(config, baseUrl, logger) | : base(config, baseUrl, logger) | ||||
{ | { | ||||
_serializer = new ETFWriter(new MemoryStream()); | _serializer = new ETFWriter(new MemoryStream()); | ||||
@@ -8,7 +8,7 @@ namespace Discord.Net.Rest | |||||
{ | { | ||||
private JsonSerializer _serializer; | private JsonSerializer _serializer; | ||||
public JsonRestClient(DiscordConfig config, string baseUrl, Logger logger) | |||||
public JsonRestClient(DiscordConfig config, string baseUrl, ILogger logger = null) | |||||
: base(config, baseUrl, logger) | : base(config, baseUrl, logger) | ||||
{ | { | ||||
_serializer = new JsonSerializer(); | _serializer = new JsonSerializer(); | ||||
@@ -38,10 +38,9 @@ namespace Discord.Net.Rest | |||||
private readonly DiscordConfig _config; | private readonly DiscordConfig _config; | ||||
private readonly IRestEngine _engine; | private readonly IRestEngine _engine; | ||||
private readonly ETFWriter _serializer; | private readonly ETFWriter _serializer; | ||||
private readonly ILogger _logger; | |||||
private string _token; | private string _token; | ||||
internal Logger Logger { get; } | |||||
public CancellationToken CancelToken { get; set; } | public CancellationToken CancelToken { get; set; } | ||||
public string Token | public string Token | ||||
@@ -54,10 +53,10 @@ namespace Discord.Net.Rest | |||||
} | } | ||||
} | } | ||||
protected RestClient(DiscordConfig config, string baseUrl, Logger logger) | |||||
protected RestClient(DiscordConfig config, string baseUrl, ILogger logger = null) | |||||
{ | { | ||||
_config = config; | _config = config; | ||||
Logger = logger; | |||||
_logger = logger; | |||||
#if !DOTNET5_4 | #if !DOTNET5_4 | ||||
_engine = new RestSharpEngine(config, baseUrl, logger); | _engine = new RestSharpEngine(config, baseUrl, logger); | ||||
@@ -65,7 +64,7 @@ namespace Discord.Net.Rest | |||||
_engine = new BuiltInEngine(config, baseUrl, logger); | _engine = new BuiltInEngine(config, baseUrl, logger); | ||||
#endif | #endif | ||||
if (Logger.Level >= LogSeverity.Verbose) | |||||
if (_logger != null && _logger.Level >= LogSeverity.Verbose) | |||||
{ | { | ||||
this.SentRequest += (s, e) => | this.SentRequest += (s, e) => | ||||
{ | { | ||||
@@ -82,7 +81,7 @@ namespace Discord.Net.Rest | |||||
log += $" {e.ResponseJson}"; | log += $" {e.ResponseJson}"; | ||||
} | } | ||||
} | } | ||||
Logger.Verbose(log); | |||||
_logger.Verbose(log); | |||||
}; | }; | ||||
} | } | ||||
} | } | ||||
@@ -19,14 +19,13 @@ namespace Discord.Net.Rest | |||||
private readonly RestSharpClient _client; | private readonly RestSharpClient _client; | ||||
private readonly AsyncLock _rateLimitLock; | private readonly AsyncLock _rateLimitLock; | ||||
private readonly ILogger _logger; | |||||
private DateTime _rateLimitTime; | private DateTime _rateLimitTime; | ||||
internal Logger Logger { get; } | |||||
public RestSharpEngine(DiscordConfig config, string baseUrl, Logger logger) | |||||
public RestSharpEngine(DiscordConfig config, string baseUrl, ILogger logger) | |||||
{ | { | ||||
_config = config; | _config = config; | ||||
Logger = logger; | |||||
_logger = logger; | |||||
_rateLimitLock = new AsyncLock(); | _rateLimitLock = new AsyncLock(); | ||||
_client = new RestSharpClient(baseUrl) | _client = new RestSharpClient(baseUrl) | ||||
@@ -89,15 +88,18 @@ namespace Discord.Net.Rest | |||||
int milliseconds; | int milliseconds; | ||||
if (retryAfter != null && int.TryParse((string)retryAfter.Value, out milliseconds)) | if (retryAfter != null && int.TryParse((string)retryAfter.Value, out milliseconds)) | ||||
{ | { | ||||
var now = DateTime.UtcNow; | |||||
if (now >= _rateLimitTime) | |||||
if (_logger != null) | |||||
{ | { | ||||
using (await _rateLimitLock.LockAsync().ConfigureAwait(false)) | |||||
var now = DateTime.UtcNow; | |||||
if (now >= _rateLimitTime) | |||||
{ | { | ||||
if (now >= _rateLimitTime) | |||||
using (await _rateLimitLock.LockAsync().ConfigureAwait(false)) | |||||
{ | { | ||||
_rateLimitTime = now.AddMilliseconds(milliseconds); | |||||
Logger.Warning($"Rate limit hit, waiting {Math.Round(milliseconds / 1000.0f, 2)} seconds"); | |||||
if (now >= _rateLimitTime) | |||||
{ | |||||
_rateLimitTime = now.AddMilliseconds(milliseconds); | |||||
_logger.Warning($"Rate limit hit, waiting {Math.Round(milliseconds / 1000.0f, 2)} seconds"); | |||||
} | |||||
} | } | ||||
} | } | ||||
} | } | ||||