1
0
Fork 0
mirror of https://github.com/Jackett/Jackett synced 2025-01-18 21:50:33 +00:00

WebClient: improve logging

This commit is contained in:
kaso17 2017-11-15 19:00:27 +01:00
parent 798dc9bf4b
commit dd09daf28f

View file

@ -21,6 +21,7 @@ namespace Jackett.Utils.Clients
protected IProcessService processService; protected IProcessService processService;
protected DateTime lastRequest = DateTime.MinValue; protected DateTime lastRequest = DateTime.MinValue;
protected TimeSpan requestDelayTimeSpan; protected TimeSpan requestDelayTimeSpan;
protected string ClientType;
public bool EmulateBrowser = true; public bool EmulateBrowser = true;
public double requestDelay public double requestDelay
{ {
@ -42,6 +43,7 @@ namespace Jackett.Utils.Clients
logger = l; logger = l;
configService = c; configService = c;
serverConfig = sc; serverConfig = sc;
ClientType = GetType().Name;
} }
async protected Task DelayRequest(WebRequest request) async protected Task DelayRequest(WebRequest request)
@ -55,7 +57,7 @@ namespace Jackett.Utils.Clients
if (timeElapsed < requestDelayTimeSpan) if (timeElapsed < requestDelayTimeSpan)
{ {
var delay = requestDelayTimeSpan - timeElapsed; var delay = requestDelayTimeSpan - timeElapsed;
logger.Debug(string.Format("IWebClient: delaying request for {0} by {1} seconds", request.Url, delay.TotalSeconds.ToString())); logger.Debug(string.Format("WebClient({0}): delaying request for {1} by {2} seconds", ClientType, request.Url, delay.TotalSeconds.ToString()));
await Task.Delay(delay); await Task.Delay(delay);
} }
lastRequest = DateTime.Now; lastRequest = DateTime.Now;
@ -92,18 +94,18 @@ namespace Jackett.Utils.Clients
virtual public async Task<WebClientByteResult> GetBytes(WebRequest request) virtual public async Task<WebClientByteResult> GetBytes(WebRequest request)
{ {
logger.Debug(string.Format("IWebClient.GetBytes(Url:{0})", request.Url)); logger.Debug(string.Format("WebClient({0}).GetBytes(Url:{1})", ClientType, request.Url));
PrepareRequest(request); PrepareRequest(request);
await DelayRequest(request); await DelayRequest(request);
var result = await Run(request); var result = await Run(request);
result.Request = request; result.Request = request;
logger.Debug(string.Format("IWebClient: Returning {0} => {1} bytes", result.Status, (result.IsRedirect ? result.RedirectingTo + " " : "") + (result.Content == null ? "<NULL>" : result.Content.Length.ToString()))); logger.Debug(string.Format("WebClient({0}): Returning {1} => {2} bytes", ClientType, result.Status, (result.IsRedirect ? result.RedirectingTo + " " : "") + (result.Content == null ? "<NULL>" : result.Content.Length.ToString())));
return result; return result;
} }
virtual public async Task<WebClientStringResult> GetString(WebRequest request) virtual public async Task<WebClientStringResult> GetString(WebRequest request)
{ {
logger.Debug(string.Format("IWebClient.GetString(Url:{0})", request.Url)); logger.Debug(string.Format("WebClient({0}).GetString(Url:{1})", ClientType, request.Url));
PrepareRequest(request); PrepareRequest(request);
await DelayRequest(request); await DelayRequest(request);
var result = await Run(request); var result = await Run(request);
@ -127,18 +129,18 @@ namespace Jackett.Utils.Clients
} }
catch (Exception ex) catch (Exception ex)
{ {
logger.Error(string.Format("IWebClient.GetString(Url:{0}): Error loading encoding {0} based on header {1}: {2}", request.Url, charset, result.Headers["content-type"][0], ex)); logger.Error(string.Format("WebClient({0}).GetString(Url:{1}): Error loading encoding {2} based on header {3}: {4}", ClientType, request.Url, charset, result.Headers["content-type"][0], ex));
} }
} }
else else
{ {
logger.Error(string.Format("IWebClient.GetString(Url:{0}): Got header without charset: {0}", request.Url, result.Headers["content-type"][0])); logger.Error(string.Format("WebClient({0}).GetString(Url:{1}): Got header without charset: {2}", ClientType, request.Url, result.Headers["content-type"][0]));
} }
} }
if (encoding == null) if (encoding == null)
{ {
logger.Error(string.Format("IWebClient.GetString(Url:{0}): No encoding detected, defaulting to UTF-8", request.Url)); logger.Error(string.Format("WebClient({0}).GetString(Url:{1}): No encoding detected, defaulting to UTF-8", ClientType, request.Url));
encoding = Encoding.UTF8; encoding = Encoding.UTF8;
} }
@ -147,7 +149,7 @@ namespace Jackett.Utils.Clients
decodedContent = encoding.GetString(result.Content); decodedContent = encoding.GetString(result.Content);
stringResult.Content = decodedContent; stringResult.Content = decodedContent;
logger.Debug(string.Format("IWebClient: Returning {0} => {1}", result.Status, (result.IsRedirect ? result.RedirectingTo + " " : "") + (decodedContent == null ? "<NULL>" : decodedContent))); logger.Debug(string.Format("WebClient({0}): Returning {1} => {2}", ClientType, result.Status, (result.IsRedirect ? result.RedirectingTo + " " : "") + (decodedContent == null ? "<NULL>" : decodedContent)));
string[] server; string[] server;
if (stringResult.Headers.TryGetValue("server", out server)) if (stringResult.Headers.TryGetValue("server", out server))