From ba1e9e961d9f6143da83b152455fb4115e17e67f Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Thu, 10 Aug 2023 22:42:31 +0530 Subject: [PATCH] Updated ablyrest and realtime instances to use internal logger --- src/IO.Ably.Android/Platform.cs | 4 ++-- src/IO.Ably.NETFramework/Platform.cs | 4 ++-- src/IO.Ably.NETStandard20/Platform.cs | 4 ++-- src/IO.Ably.Shared/AblyRealtime.cs | 12 ++++-------- src/IO.Ably.Shared/AblyRest.cs | 12 ++++-------- src/IO.Ably.Shared/EventEmitter.cs | 2 +- src/IO.Ably.Shared/IPlatform.cs | 3 ++- src/IO.Ably.Shared/IoC.cs | 2 +- src/IO.Ably.Shared/Realtime/Connection.cs | 13 +++---------- src/IO.Ably.iOS/Platform.cs | 4 ++-- 10 files changed, 23 insertions(+), 37 deletions(-) diff --git a/src/IO.Ably.Android/Platform.cs b/src/IO.Ably.Android/Platform.cs index 73ff9f369..341ce5677 100644 --- a/src/IO.Ably.Android/Platform.cs +++ b/src/IO.Ably.Android/Platform.cs @@ -16,14 +16,14 @@ internal class Platform : IPlatform public IMobileDevice MobileDevice { get; set; } - public void RegisterOsNetworkStateChanged() + public void RegisterOsNetworkStateChanged(ILogger logger) { lock (_lock) { if (HookedUpToNetworkEvents == false) { NetworkChange.NetworkAvailabilityChanged += (sender, eventArgs) => - Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline); + Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline, logger); } HookedUpToNetworkEvents = true; diff --git a/src/IO.Ably.NETFramework/Platform.cs b/src/IO.Ably.NETFramework/Platform.cs index f6765f3e6..a2527929e 100644 --- a/src/IO.Ably.NETFramework/Platform.cs +++ b/src/IO.Ably.NETFramework/Platform.cs @@ -27,14 +27,14 @@ internal static void Initialize() HookedUpToNetworkEvents = false; } - public void RegisterOsNetworkStateChanged() + public void RegisterOsNetworkStateChanged(ILogger logger) { lock (_lock) { if (HookedUpToNetworkEvents == false) { NetworkChange.NetworkAvailabilityChanged += (sender, eventArgs) => - Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline); + Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline, logger); } HookedUpToNetworkEvents = true; diff --git a/src/IO.Ably.NETStandard20/Platform.cs b/src/IO.Ably.NETStandard20/Platform.cs index 72eeaa9b5..1333c84ce 100644 --- a/src/IO.Ably.NETStandard20/Platform.cs +++ b/src/IO.Ably.NETStandard20/Platform.cs @@ -34,14 +34,14 @@ internal static void Initialize() HookedUpToNetworkEvents = false; } - public void RegisterOsNetworkStateChanged() + public void RegisterOsNetworkStateChanged(ILogger logger) { lock (Lock) { if (HookedUpToNetworkEvents == false) { NetworkChange.NetworkAvailabilityChanged += (sender, eventArgs) => - Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline); + Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline, logger); } HookedUpToNetworkEvents = true; diff --git a/src/IO.Ably.Shared/AblyRealtime.cs b/src/IO.Ably.Shared/AblyRealtime.cs index 945a2dfe4..c7bd15818 100644 --- a/src/IO.Ably.Shared/AblyRealtime.cs +++ b/src/IO.Ably.Shared/AblyRealtime.cs @@ -19,7 +19,7 @@ public class AblyRealtime : IRealtimeClient, IDisposable { private SynchronizationContext _synchronizationContext; - internal ILogger Logger { get; set; } = DefaultLogger.LoggerInstance; + internal ILogger Logger { get; set; } internal RealtimeWorkflow Workflow { get; private set; } @@ -51,11 +51,7 @@ internal AblyRealtime(ClientOptions options, IMobileDevice mobileDevice) internal AblyRealtime(ClientOptions options, Func createRestFunc, IMobileDevice mobileDevice = null) { - if (options.Logger != null) - { - Logger = options.Logger; - } - + Logger = options.Logger; Logger.LogLevel = options.LogLevel; if (options.LogHandler != null) @@ -67,12 +63,12 @@ internal AblyRealtime(ClientOptions options, FuncInitializes the rest client and validates the passed in options. private void InitializeAbly(IMobileDevice mobileDevice) { + Logger = Options.Logger; + Logger.LogLevel = Options.LogLevel; + if (Options == null) { Logger.Error("No options provider to Ably rest"); throw new AblyException("Invalid options"); } - if (Options.Logger != null) - { - Logger = Options.Logger; - } - - Logger.LogLevel = Options.LogLevel; - if (Options.LogHandler != null) { Logger.LoggerSink = Options.LogHandler; diff --git a/src/IO.Ably.Shared/EventEmitter.cs b/src/IO.Ably.Shared/EventEmitter.cs index 7ae0daef2..9d70aaa24 100644 --- a/src/IO.Ably.Shared/EventEmitter.cs +++ b/src/IO.Ably.Shared/EventEmitter.cs @@ -109,7 +109,7 @@ public abstract class EventEmitter : IEventEmitter { internal EventEmitter(ILogger logger) { - Logger = logger ?? DefaultLogger.LoggerInstance; + Logger = logger; } internal ILogger Logger { get; set; } diff --git a/src/IO.Ably.Shared/IPlatform.cs b/src/IO.Ably.Shared/IPlatform.cs index d90983244..65fadb531 100644 --- a/src/IO.Ably.Shared/IPlatform.cs +++ b/src/IO.Ably.Shared/IPlatform.cs @@ -29,6 +29,7 @@ internal interface IPlatform /// The implementation will only allow one registration to operating system network state events even /// thought this method can be called multiple times. /// - void RegisterOsNetworkStateChanged(); + /// Logger provided by the realtime client. + void RegisterOsNetworkStateChanged(ILogger logger); } } diff --git a/src/IO.Ably.Shared/IoC.cs b/src/IO.Ably.Shared/IoC.cs index a16fe0f25..ac39f9de5 100644 --- a/src/IO.Ably.Shared/IoC.cs +++ b/src/IO.Ably.Shared/IoC.cs @@ -37,7 +37,7 @@ static IoC() public static ITransportFactory TransportFactory => Platform?.TransportFactory ?? new MsWebSocketTransport.TransportFactory(); - public static void RegisterOsNetworkStateChanged() => Platform.RegisterOsNetworkStateChanged(); + public static void RegisterOsNetworkStateChanged(ILogger logger) => Platform.RegisterOsNetworkStateChanged(logger); public static Agent.PlatformRuntime PlatformId => Platform?.PlatformId ?? Agent.PlatformRuntime.Other; diff --git a/src/IO.Ably.Shared/Realtime/Connection.cs b/src/IO.Ably.Shared/Realtime/Connection.cs index f2bf3f4f8..6727afc96 100644 --- a/src/IO.Ably.Shared/Realtime/Connection.cs +++ b/src/IO.Ably.Shared/Realtime/Connection.cs @@ -45,16 +45,9 @@ public sealed class Connection : EventEmitter /// The current state of the OS network connection. - public static void NotifyOperatingSystemNetworkState(NetworkState state) => - NotifyOperatingSystemNetworkState(state, null); - + /// Logger provided by the realtime client. internal static void NotifyOperatingSystemNetworkState(NetworkState state, ILogger logger) { - if (logger == null) - { - logger = DefaultLogger.LoggerInstance; - } - if (logger.IsDebug) { logger.Debug("OS Network connection state: " + state); @@ -119,8 +112,8 @@ private void CleanUpNetworkStateEvents() || State == Realtime.ConnectionState.Disconnected) && RealtimeClient.Options.QueueMessages); - internal Connection(AblyRealtime realtimeClient, Func nowFunc, ILogger logger = null) - : base(logger) + internal Connection(AblyRealtime realtimeClient, Func nowFunc) + : base(realtimeClient.Logger) { Now = nowFunc; RealtimeClient = realtimeClient; diff --git a/src/IO.Ably.iOS/Platform.cs b/src/IO.Ably.iOS/Platform.cs index 6c65efa27..05290245c 100644 --- a/src/IO.Ably.iOS/Platform.cs +++ b/src/IO.Ably.iOS/Platform.cs @@ -14,14 +14,14 @@ internal class Platform : IPlatform public ITransportFactory TransportFactory => null; public IMobileDevice MobileDevice { get; set; } - public void RegisterOsNetworkStateChanged() + public void RegisterOsNetworkStateChanged(ILogger logger) { lock (_lock) { if (HookedUpToNetworkEvents == false) { NetworkChange.NetworkAvailabilityChanged += (sender, eventArgs) => - Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline); + Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline, logger); } HookedUpToNetworkEvents = true;