diff --git a/CryptoExchange.Net.UnitTests/ExchangeClientTests.cs b/CryptoExchange.Net.UnitTests/ExchangeClientTests.cs index b9be9e6..e19af84 100644 --- a/CryptoExchange.Net.UnitTests/ExchangeClientTests.cs +++ b/CryptoExchange.Net.UnitTests/ExchangeClientTests.cs @@ -177,7 +177,7 @@ namespace CryptoExchange.Net.UnitTests { ApiCredentials = new ApiCredentials("Test", "Test2"), LogVerbosity = verbosity, - LogWriter = tw + LogWriters = new List() { tw } }); } else diff --git a/CryptoExchange.Net/CryptoExchange.Net.csproj b/CryptoExchange.Net/CryptoExchange.Net.csproj index dae3b75..9979697 100644 --- a/CryptoExchange.Net/CryptoExchange.Net.csproj +++ b/CryptoExchange.Net/CryptoExchange.Net.csproj @@ -7,7 +7,7 @@ CryptoExchange.Net JKorf - 0.0.12 + 0.0.13 false https://github.com/JKorf/CryptoExchange.Net https://github.com/JKorf/CryptoExchange.Net/blob/master/LICENSE diff --git a/CryptoExchange.Net/ExchangeClient.cs b/CryptoExchange.Net/ExchangeClient.cs index 862d60e..d7a6b6c 100644 --- a/CryptoExchange.Net/ExchangeClient.cs +++ b/CryptoExchange.Net/ExchangeClient.cs @@ -38,10 +38,9 @@ namespace CryptoExchange.Net /// Options protected void Configure(ExchangeOptions exchangeOptions) { - if (exchangeOptions.LogWriter != null) - log.TextWriter = exchangeOptions.LogWriter; - + log.UpdateWriters(exchangeOptions.LogWriters); log.Level = exchangeOptions.LogVerbosity; + apiProxy = exchangeOptions.Proxy; if(apiProxy != null) log.Write(LogVerbosity.Info, $"Setting api proxy to {exchangeOptions.Proxy.Host}:{exchangeOptions.Proxy.Port}"); @@ -292,6 +291,7 @@ namespace CryptoExchange.Net public virtual void Dispose() { + log.Write(LogVerbosity.Debug, "Disposing exchange client"); } } } diff --git a/CryptoExchange.Net/ExchangeOptions.cs b/CryptoExchange.Net/ExchangeOptions.cs index 3dde450..b2629da 100644 --- a/CryptoExchange.Net/ExchangeOptions.cs +++ b/CryptoExchange.Net/ExchangeOptions.cs @@ -28,9 +28,9 @@ namespace CryptoExchange.Net public LogVerbosity LogVerbosity { get; set; } = LogVerbosity.Info; /// - /// The log writer + /// The log writers /// - public TextWriter LogWriter { get; set; } = new DebugTextWriter(); + public List LogWriters { get; set; } = new List() {new DebugTextWriter()}; /// /// List of ratelimiters to use diff --git a/CryptoExchange.Net/Implementation/BaseSocket.cs b/CryptoExchange.Net/Implementation/BaseSocket.cs index e6c975f..1e8e6e6 100644 --- a/CryptoExchange.Net/Implementation/BaseSocket.cs +++ b/CryptoExchange.Net/Implementation/BaseSocket.cs @@ -23,6 +23,18 @@ namespace CryptoExchange.Net.Implementation public bool IsClosed => socket.State == WebSocketState.Closed; public bool IsOpen => socket.State == WebSocketState.Open; + public bool PingConnection + { + get => socket.EnableAutoSendPing; + set => socket.EnableAutoSendPing = value; + } + + public TimeSpan PingInterval + { + get => TimeSpan.FromSeconds(socket.AutoSendPingInterval); + set => socket.AutoSendPingInterval = (int) Math.Round(value.TotalSeconds); + } + public BaseSocket(string url):this(url, new Dictionary(), new Dictionary()) { } @@ -36,6 +48,8 @@ namespace CryptoExchange.Net.Implementation socket.Closed += (o, s) => Handle(closehandlers); socket.Error += (o, s) => Handle(errorhandlers, s.Exception); socket.MessageReceived += (o, s) => Handle(messagehandlers, s.Message); + socket.EnableAutoSendPing = true; + socket.AutoSendPingInterval = 10; } public event Action OnClose diff --git a/CryptoExchange.Net/Interfaces/IWebsocket.cs b/CryptoExchange.Net/Interfaces/IWebsocket.cs index 1dc7771..9b5ac84 100644 --- a/CryptoExchange.Net/Interfaces/IWebsocket.cs +++ b/CryptoExchange.Net/Interfaces/IWebsocket.cs @@ -15,6 +15,8 @@ namespace CryptoExchange.Net.Interfaces bool IsClosed { get; } bool IsOpen { get; } + bool PingConnection { get; set; } + TimeSpan PingInterval { get; set; } Task Connect(); void Send(string data); diff --git a/CryptoExchange.Net/Logging/Log.cs b/CryptoExchange.Net/Logging/Log.cs index fbd2ef3..0f88f16 100644 --- a/CryptoExchange.Net/Logging/Log.cs +++ b/CryptoExchange.Net/Logging/Log.cs @@ -1,11 +1,14 @@ using System; +using System.Collections.Generic; +using System.Diagnostics; using System.IO; +using System.Linq; namespace CryptoExchange.Net.Logging { public class Log { - public TextWriter TextWriter { get; internal set; } = new DebugTextWriter(); + private List writers; private LogVerbosity level = LogVerbosity.Info; public LogVerbosity Level @@ -21,10 +24,30 @@ namespace CryptoExchange.Net.Logging } } + public Log() + { + writers = new List(); + } + + public void UpdateWriters(List textWriters) + { + writers = textWriters; + } + public void Write(LogVerbosity logType, string message) { - if ((int)logType >= (int)Level) - TextWriter.WriteLine($"{DateTime.Now:hh:mm:ss:fff} | {logType} | {message}"); + foreach (var writer in writers) + { + try + { + if ((int) logType >= (int) Level) + writer.WriteLine($"{DateTime.Now:yyyy/MM/dd hh:mm:ss:fff} | {logType} | {message}"); + } + catch (Exception e) + { + Debug.WriteLine("Failed to write log: " + e.Message); + } + } } }