diff --git a/src/Api/PubnubApi/Pubnub.cs b/src/Api/PubnubApi/Pubnub.cs index 13bdcaf6..23fda596 100644 --- a/src/Api/PubnubApi/Pubnub.cs +++ b/src/Api/PubnubApi/Pubnub.cs @@ -958,7 +958,7 @@ public void SetJsonPluggableLibrary(IJsonPluggableLibrary customJson) #endregion #region "Constructors" - public Pubnub(PNConfiguration config) + public Pubnub(PNConfiguration config, IHttpClientService httpTransportService = default, ITransportMiddleware middleware = default) { if (config == null) { @@ -995,8 +995,8 @@ public Pubnub(PNConfiguration config) CheckRequiredUserId(config); eventEmitter = new EventEmitter(pubnubConfig.ContainsKey(InstanceId) ? pubnubConfig[InstanceId] : null, subscribeCallbackListenerList, JsonPluggableLibrary, tokenManager, pubnubLog, this); CheckCryptoModuleUsageForLogging(config); - IHttpClientService httpClientService = new HttpClientService(config.Proxy); - transportMiddleware = new Middleware(httpClientService,config, this, tokenManager); + IHttpClientService httpClientService = httpTransportService ?? new HttpClientService(proxy:config.Proxy, pubnubLog: config.PubnubLog, verbosity: config.LogVerbosity); + transportMiddleware = middleware ?? new Middleware(httpClientService,config, this, tokenManager); } #if UNITY diff --git a/src/Api/PubnubApi/Transport/HttpClientService.cs b/src/Api/PubnubApi/Transport/HttpClientService.cs index d48e0f1b..4abc4fc6 100644 --- a/src/Api/PubnubApi/Transport/HttpClientService.cs +++ b/src/Api/PubnubApi/Transport/HttpClientService.cs @@ -1,4 +1,5 @@ using System; +using System.Globalization; using System.Net.Http; using System.Threading.Tasks; using System.Linq; @@ -11,7 +12,9 @@ namespace PubnubApi public class HttpClientService : IHttpClientService { private readonly HttpClient httpClient; - public HttpClientService(IWebProxy proxy = default) + private IPubnubLog Log { get; set; } + private PNLogVerbosity LogVerbosity { get; set; } + public HttpClientService(IWebProxy proxy = default, IPubnubLog pubnubLog = default, PNLogVerbosity verbosity = default) { httpClient = new HttpClient() { @@ -24,6 +27,8 @@ public HttpClientService(IWebProxy proxy = default) UseProxy = true }); httpClient.Timeout = Timeout.InfiniteTimeSpan; + Log = pubnubLog; + LogVerbosity = verbosity; } public async Task GetRequest(TransportRequest transportRequest) @@ -44,10 +49,11 @@ public async Task GetRequest(TransportRequest transportReques Headers = httpResult.Headers.ToDictionary(h => h.Key, h => h.Value), RequestUrl = httpResult.RequestMessage?.RequestUri?.AbsolutePath }; - } catch (Exception ex) { + } catch (Exception e) { + LoggingMethod.WriteToLog(Log, $"{DateTime.Now.ToString(CultureInfo.InvariantCulture)}: Exception for http call url {transportRequest.RequestUrl}, exception message: {e.Message}, stacktrace: {e.StackTrace}", LogVerbosity); response = new TransportResponse() { RequestUrl = transportRequest.RequestUrl, - Error = ex + Error = e }; } return response; @@ -80,6 +86,7 @@ public async Task PostRequest(TransportRequest transportReque RequestUrl = httpResult.RequestMessage?.RequestUri?.AbsolutePath }; } catch (Exception e) { + LoggingMethod.WriteToLog(Log, $"{DateTime.Now.ToString(CultureInfo.InvariantCulture)}: Exception for http call url {transportRequest.RequestUrl}, exception message: {e.Message}, stacktrace: {e.StackTrace}", LogVerbosity); transportResponse = new TransportResponse() { RequestUrl = transportRequest.RequestUrl, Error = e @@ -132,6 +139,7 @@ public async Task PutRequest(TransportRequest transportReques } catch (Exception e) { + LoggingMethod.WriteToLog(Log, $"{DateTime.Now.ToString(CultureInfo.InvariantCulture)}: Exception for http call url {transportRequest.RequestUrl}, exception message: {e.Message}, stacktrace: {e.StackTrace}", LogVerbosity); transportResponse = new TransportResponse() { RequestUrl = transportRequest.RequestUrl, Error = e @@ -159,10 +167,11 @@ public async Task DeleteRequest(TransportRequest transportReq Headers = httpResult.Headers.ToDictionary(h => h.Key, h => h.Value), RequestUrl = httpResult.RequestMessage?.RequestUri?.AbsolutePath }; - } catch (Exception ex) { + } catch (Exception e) { + LoggingMethod.WriteToLog(Log, $"{DateTime.Now.ToString(CultureInfo.InvariantCulture)}: Exception for http call url {transportRequest.RequestUrl}, exception message: {e.Message}, stacktrace: {e.StackTrace}", LogVerbosity); response = new TransportResponse() { RequestUrl = transportRequest.RequestUrl, - Error = ex + Error = e }; } return response; @@ -213,6 +222,7 @@ public async Task PatchRequest(TransportRequest transportRequ } catch (Exception e) { + LoggingMethod.WriteToLog(Log, $"{DateTime.Now.ToString(CultureInfo.InvariantCulture)}: Exception for http call url {transportRequest.RequestUrl}, exception message: {e.Message}, stacktrace: {e.StackTrace}", LogVerbosity); transportResponse = new TransportResponse() { RequestUrl = transportRequest.RequestUrl, Error = e