1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-08 16:36:15 +00:00

Additional logging, added ThreadSafeFileWriter

This commit is contained in:
JKorf 2018-05-03 09:58:34 +02:00
parent 4366f4082a
commit 6c23487d21
6 changed files with 65 additions and 10 deletions

View File

@ -79,13 +79,17 @@ namespace CryptoExchange.Net
protected virtual async Task<CallResult<T>> ExecuteRequest<T>(Uri uri, string method = "GET", Dictionary<string, object> parameters = null, bool signed = false) where T : class protected virtual async Task<CallResult<T>> ExecuteRequest<T>(Uri uri, string method = "GET", Dictionary<string, object> parameters = null, bool signed = false) where T : class
{ {
log.Write(LogVerbosity.Debug, $"Creating request for " + uri);
if (signed && authProvider == null) if (signed && authProvider == null)
return new CallResult<T>(null, new NoApiCredentialsError()); return new CallResult<T>(null, new NoApiCredentialsError());
var request = ConstructRequest(uri, method, parameters, signed); var request = ConstructRequest(uri, method, parameters, signed);
if (apiProxy != null) if (apiProxy != null)
{
log.Write(LogVerbosity.Debug, "Setting proxy");
request.SetProxy(apiProxy.Host, apiProxy.Port); request.SetProxy(apiProxy.Host, apiProxy.Port);
}
foreach (var limiter in rateLimiters) foreach (var limiter in rateLimiters)
{ {

View File

@ -7,6 +7,7 @@ using System.Security.Authentication;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using CryptoExchange.Net.Interfaces; using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Logging;
using SuperSocket.ClientEngine.Proxy; using SuperSocket.ClientEngine.Proxy;
using WebSocket4Net; using WebSocket4Net;
@ -15,6 +16,7 @@ namespace CryptoExchange.Net.Implementation
public class BaseSocket: IWebsocket public class BaseSocket: IWebsocket
{ {
protected WebSocket socket; protected WebSocket socket;
protected Log log;
protected object socketLock = new object(); protected object socketLock = new object();
protected readonly List<Action<Exception>> errorhandlers = new List<Action<Exception>>(); protected readonly List<Action<Exception>> errorhandlers = new List<Action<Exception>>();
@ -37,12 +39,13 @@ namespace CryptoExchange.Net.Implementation
set => socket.AutoSendPingInterval = (int) Math.Round(value.TotalSeconds); set => socket.AutoSendPingInterval = (int) Math.Round(value.TotalSeconds);
} }
public BaseSocket(string url):this(url, new Dictionary<string, string>(), new Dictionary<string, string>()) public BaseSocket(Log log, string url):this(log, url, new Dictionary<string, string>(), new Dictionary<string, string>())
{ {
} }
public BaseSocket(string url, IDictionary<string, string> cookies, IDictionary<string, string> headers) public BaseSocket(Log log, string url, IDictionary<string, string> cookies, IDictionary<string, string> headers)
{ {
this.log = log;
socket = new WebSocket(url, cookies: cookies.ToList(), customHeaderItems: headers.ToList()); socket = new WebSocket(url, cookies: cookies.ToList(), customHeaderItems: headers.ToList());
socket.EnableAutoSendPing = true; socket.EnableAutoSendPing = true;
socket.AutoSendPingInterval = 10; socket.AutoSendPingInterval = 10;
@ -96,6 +99,7 @@ namespace CryptoExchange.Net.Implementation
if (socket == null || IsClosed) if (socket == null || IsClosed)
return; return;
log.Write(LogVerbosity.Debug, "Closing websocket");
ManualResetEvent evnt = new ManualResetEvent(false); ManualResetEvent evnt = new ManualResetEvent(false);
var handler = new EventHandler((o, a) => evnt.Set()); var handler = new EventHandler((o, a) => evnt.Set());
socket.Closed += handler; socket.Closed += handler;
@ -104,7 +108,9 @@ namespace CryptoExchange.Net.Implementation
socket.Closed -= handler; socket.Closed -= handler;
if (!triggered) if (!triggered)
Debug.WriteLine($"Not triggered, {socket.State}, Open: {IsOpen}, Closed: {IsClosed}"); log.Write(LogVerbosity.Debug, "Websocket closed event did not trigger");
else
log.Write(LogVerbosity.Debug, "Websocket closed");
} }
}).ConfigureAwait(false); }).ConfigureAwait(false);
} }
@ -120,6 +126,7 @@ namespace CryptoExchange.Net.Implementation
{ {
lock (socketLock) lock (socketLock)
{ {
log.Write(LogVerbosity.Debug, "Connecting websocket");
ManualResetEvent evnt = new ManualResetEvent(false); ManualResetEvent evnt = new ManualResetEvent(false);
var handler = new EventHandler((o, a) => evnt.Set()); var handler = new EventHandler((o, a) => evnt.Set());
socket.Opened += handler; socket.Opened += handler;
@ -128,7 +135,12 @@ namespace CryptoExchange.Net.Implementation
evnt.WaitOne(); evnt.WaitOne();
socket.Opened -= handler; socket.Opened -= handler;
socket.Closed -= handler; socket.Closed -= handler;
return socket.State == WebSocketState.Open; var connected = socket.State == WebSocketState.Open;
if (connected)
log.Write(LogVerbosity.Debug, "Websocket connected");
else
log.Write(LogVerbosity.Debug, "Websocket connection failed, state: " + socket.State);
return connected;
} }
}).ConfigureAwait(false); }).ConfigureAwait(false);
} }
@ -150,6 +162,9 @@ namespace CryptoExchange.Net.Implementation
{ {
lock (socketLock) lock (socketLock)
{ {
if (socket != null)
log.Write(LogVerbosity.Debug, "Disposing websocket");
socket?.Dispose(); socket?.Dispose();
socket = null; socket = null;

View File

@ -1,12 +1,13 @@
using CryptoExchange.Net.Interfaces; using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Logging;
namespace CryptoExchange.Net.Implementation namespace CryptoExchange.Net.Implementation
{ {
public class WebsocketFactory : IWebsocketFactory public class WebsocketFactory : IWebsocketFactory
{ {
public IWebsocket CreateWebsocket(string url) public IWebsocket CreateWebsocket(Log log, string url)
{ {
return new BaseSocket(url); return new BaseSocket(log, url);
} }
} }
} }

View File

@ -1,7 +1,9 @@
namespace CryptoExchange.Net.Interfaces using CryptoExchange.Net.Logging;
namespace CryptoExchange.Net.Interfaces
{ {
public interface IWebsocketFactory public interface IWebsocketFactory
{ {
IWebsocket CreateWebsocket(string url); IWebsocket CreateWebsocket(Log log, string url);
} }
} }

View File

@ -0,0 +1,33 @@
using System.IO;
using System.Text;
namespace CryptoExchange.Net.Logging
{
public class ThreadSafeFileWriter: TextWriter
{
private StreamWriter logWriter;
private object writeLock;
public override Encoding Encoding => Encoding.ASCII;
public ThreadSafeFileWriter(string path)
{
writeLock = new object();
logWriter = new StreamWriter(File.Open(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read));
logWriter.AutoFlush = true;
}
public override void WriteLine(string logMessage)
{
lock (writeLock)
logWriter.WriteLine(logMessage);
}
protected override void Dispose(bool disposing)
{
logWriter.Close();
logWriter = null;
}
}
}

View File

@ -53,7 +53,7 @@ namespace CryptoExchange.Net.Requests
public async Task<Stream> GetRequestStream() public async Task<Stream> GetRequestStream()
{ {
return await request.GetRequestStreamAsync(); return await request.GetRequestStreamAsync().ConfigureAwait(false);
} }
public async Task<IResponse> GetResponse() public async Task<IResponse> GetResponse()