Added ILogger, made logger optional for RestClients

This commit is contained in:
RogueException
2016-02-07 16:38:17 -04:00
parent 18ae9f6bc8
commit 937ff71c4a
8 changed files with 79 additions and 28 deletions

View File

@@ -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>

View File

@@ -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);
}
}

View File

@@ -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;

View File

@@ -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
@@ -99,6 +99,8 @@ namespace Discord.Net.Rest
int milliseconds; int milliseconds;
if (retryAfter != null && int.TryParse(retryAfter, out milliseconds)) if (retryAfter != null && int.TryParse(retryAfter, out milliseconds))
{
if (_logger != null)
{ {
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
if (now >= _rateLimitTime) if (now >= _rateLimitTime)
@@ -108,7 +110,8 @@ namespace Discord.Net.Rest
if (now >= _rateLimitTime) if (now >= _rateLimitTime)
{ {
_rateLimitTime = now.AddMilliseconds(milliseconds); _rateLimitTime = now.AddMilliseconds(milliseconds);
Logger.Warning($"Rate limit hit, waiting {Math.Round(milliseconds / 1000.0f, 2)} seconds"); _logger.Warning($"Rate limit hit, waiting {Math.Round(milliseconds / 1000.0f, 2)} seconds");
}
} }
} }
} }

View File

@@ -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());

View File

@@ -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();

View File

@@ -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);
}; };
} }
} }

View File

@@ -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, ILogger logger)
public RestSharpEngine(DiscordConfig config, string baseUrl, Logger logger)
{ {
_config = config; _config = config;
Logger = logger; _logger = logger;
_rateLimitLock = new AsyncLock(); _rateLimitLock = new AsyncLock();
_client = new RestSharpClient(baseUrl) _client = new RestSharpClient(baseUrl)
@@ -88,6 +87,8 @@ 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))
{
if (_logger != null)
{ {
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
if (now >= _rateLimitTime) if (now >= _rateLimitTime)
@@ -97,7 +98,8 @@ namespace Discord.Net.Rest
if (now >= _rateLimitTime) if (now >= _rateLimitTime)
{ {
_rateLimitTime = now.AddMilliseconds(milliseconds); _rateLimitTime = now.AddMilliseconds(milliseconds);
Logger.Warning($"Rate limit hit, waiting {Math.Round(milliseconds / 1000.0f, 2)} seconds"); _logger.Warning($"Rate limit hit, waiting {Math.Round(milliseconds / 1000.0f, 2)} seconds");
}
} }
} }
} }