Skip to content

Commit

Permalink
[MT5] logging improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
vdemydiuk committed Nov 1, 2020
1 parent 318bae3 commit df579cc
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion MtApi5/MtApi5Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ public class MtApi5Client
#region Public Methods
public MtApi5Client()
{
LogConfigurator.Setup(LogProfileName);
#if (DEBUG)
const LogLevel logLevel = LogLevel.Debug;
#else
const LogLevel logLevel = LogLevel.Info;
#endif
LogConfigurator.Setup(LogProfileName, logLevel);

_mtEventHandlers[Mt5EventTypes.OnBookEvent] = ReceivedOnBookEvent;
_mtEventHandlers[Mt5EventTypes.OnTick] = ReceivedOnTickEvent;
Expand All @@ -64,6 +69,7 @@ public MtApi5Client()
///<param name="port">Port of host connection (default 8222) </param>
public void BeginConnect(string host, int port)
{
Log.Info($"BeginConnect: host = {host}, port = {port}");
Task.Factory.StartNew(() => Connect(host, port));
}

Expand All @@ -73,6 +79,7 @@ public void BeginConnect(string host, int port)
///<param name="port">Port of host connection (default 8222) </param>
public void BeginConnect(int port)
{
Log.Info($"BeginConnect: port = {port}");
Task.Factory.StartNew(() => Connect(port));
}

Expand All @@ -81,6 +88,7 @@ public void BeginConnect(int port)
///</summary>
public void BeginDisconnect()
{
Log.Info("BeginDisconnect called.");
Task.Factory.StartNew(() => Disconnect(false));
}

Expand Down Expand Up @@ -3351,6 +3359,8 @@ private void Connect(MtClient client)
{
client.Dispose();
message = string.IsNullOrEmpty(client.Host) ? $"Failed connection to localhost:{client.Port}. {e.Message}" : $"Failed connection to {client.Host}:{client.Port}. {e.Message}";

Log.Warn(message);
}

if (state == Mt5ConnectionState.Connected)
Expand All @@ -3363,6 +3373,8 @@ private void Connect(MtClient client)
_client.ServerFailed += _client_ServerFailed;
_client.MtEventReceived += _client_MtEventReceived;
message = string.IsNullOrEmpty(client.Host) ? $"Connected to localhost:{client.Port}" : $"Connected to { client.Host}:{client.Port}";

Log.Info(message);
}

_connectionState = state;
Expand Down Expand Up @@ -3477,6 +3489,8 @@ private void Disconnect(bool failed)
_connectionState = state;
}

Log.Info(message);

ConnectionStateChanged?.Invoke(this, new Mt5ConnectionEventArgs(state, message));
}

Expand All @@ -3487,6 +3501,7 @@ private T SendCommand<T>(Mt5CommandType commandType, ArrayList commandParameters
var client = Client;
if (client == null)
{
Log.Warn("SendCommand: No connection");
throw new Exception("No connection");
}

Expand All @@ -3496,16 +3511,19 @@ private T SendCommand<T>(Mt5CommandType commandType, ArrayList commandParameters
}
catch (CommunicationException ex)
{
Log.Warn($"SendCommand: {ex.Message}");
throw new Exception(ex.Message, ex);
}

if (response == null)
{
Log.Warn("SendCommand: Response from MetaTrader is null");
throw new ExecutionException(ErrorCode.ErrCustom, "Response from MetaTrader is null");
}

if (response.ErrorCode != 0)
{
Log.Warn($"SendCommand: ErrorCode = {response.ErrorCode}. {response}");
throw new ExecutionException((ErrorCode)response.ErrorCode, response.ToString());
}

Expand All @@ -3529,12 +3547,14 @@ private T SendRequest<T>(RequestBase request)

if (res == null)
{
Log.Warn("SendRequest: Response from MetaTrader is null");
throw new ExecutionException(ErrorCode.ErrCustom, "Response from MetaTrader is null");
}

var response = JsonConvert.DeserializeObject<Response<T>>(res);
if (response.ErrorCode != 0)
{
Log.Warn($"SendRequest: ErrorCode = {response.ErrorCode}. {response}");
throw new ExecutionException((ErrorCode)response.ErrorCode, response.ErrorMessage);
}

Expand Down

0 comments on commit df579cc

Please sign in to comment.